lytakyn
lytakyn

Reputation: 62

Setting Controls in Classes

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.

Upvotes: 0

Views: 52

Answers (1)

Willy David Jr
Willy David Jr

Reputation: 9151

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

Upvotes: 3

Related Questions