0

I'm trying a booking system, I want to put controls on the booking aspect. I want to use If and then cases. I want to control in such a way that if number of booking is 4, then it will throw an exception and stop inserting in the database.

public ApiResult<TimeModelExtended> SaveBooking(Booking booking)
    {
        AchimotaGCDb repo = new AchimotaGCDb();

        var result = new ApiResult<TimeModelExtended>();
        try
        {
            booking.PlayDate = getPlayDate(booking.RefCode);

            Int16 nb = getNbBooked(booking.RefCode);

            if (nb == 4)
            {
                Exception ex = new Exception();
                result.Successfull = 0;
                result.InternalError = ex.Message;
                result.Error = "Booking slot is full";
            }

            else  if (nb == 0)
            {
                booking.BookingStatus = 1;//Booked already
            }
            else
            {
                booking.BookingStatus = 0;//Reservation already
            }

            repo.Insert(booking);

            result.Successfull = 1;

            result = GetOneteeTime(booking.RefCode);

        }
        catch (Exception ex)
        {
            result.Successfull = 0;
            result.InternalError = ex.Message;
            result.Error = "Error from server";
        }
        finally
        {
            repo.Dispose();
        }

        return result;
    }

help to solve that.

Abhay Dixit
  • 998
  • 8
  • 28
lytakyn
  • 62
  • 7
  • Possible duplicate of [How to throw exception in Web API?](https://stackoverflow.com/questions/14607844/how-to-throw-exception-in-web-api) – mihai Sep 19 '17 at 06:26
  • Unrelated: `Int16` - why are you using this? – Fildor Sep 19 '17 at 07:01

1 Answers1

3

If you want to throw an exception, you need to really throw it:

if (nb == 4)
        {
           throw new Exception("Booking slot is full.");
        }

But I don't think throwing an exception is a good idea. Throwing an exception and validation is a different thing.

Here is my suggestion:

  if (nb == 4)
        {
           return result = new ApiResult<TimeModelExtended>()
                        {
                           Successfull = 0,
                           InternalError = "Other messages",
                           Error = ""Booking slot is full."
                        };
        }

This will return as result message that nothing will continue unless you satisfy that nb != 4

Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
  • Also mind that if he would throw an exception with the rest of the code as given in question, he would catch it in the same method modifying the result ... so this is completely superfluent. Your suggestion is the way to go. – Fildor Sep 19 '17 at 06:59
  • Does that mean that when the nb=4,the exception will the returned and when the nb<>4,then the booking is inserted into the database? – lytakyn Sep 22 '17 at 10:20
  • Yes, unless you still want to insert even you want to throw an exception? I can edit my answer if that's the case. – Willy David Jr Sep 22 '17 at 10:22