コード例 #1
0
        public async Task EndAsync(BookingEnd model)
        {
            var item = await context.Booking.SingleOrDefaultAsync(o => o.BookingId == model.BookingId);

            if (item == null)
            {
                throw new KeyNotFoundException($"Couldn't find a booking matching {model.BookingId}.");
            }

            if (item.EndMileage != null || item.EndDate != null)
            {
                throw new InvalidOperationException("This booking is completed.");
            }

            if (model.EndMileage < item.StartMileage)
            {
                throw new ArgumentException("Final mileage can't be lower than starting mileage.");
            }

            if (item.StartDate > model.EndDate)
            {
                throw new ArgumentException("End date can't be before start date.");
            }

            item.EndDate    = model.EndDate;
            item.EndMileage = model.EndMileage;

            await context.SaveChangesAsync();
        }
コード例 #2
0
        public async Task <ActionResult> Put([FromBody] BookingEnd booking)
        {
            try
            {
                await bookings.EndAsync(booking);

                return(NoContent());
            }
            catch (KeyNotFoundException e)
            {
                return(NotFound(new { error = e.Message }));
            }
            catch (InvalidOperationException e)
            {
                return(BadRequest(new { error = e.Message }));
            }
            catch (ArgumentException e)
            {
                return(BadRequest(new { error = e.Message }));
            }
        }