コード例 #1
0
        public async Task <IActionResult> UnSubscribe(int eventId)
        {
            Event @event = await EventServices.GetEventByIdAsync(eventId);

            Booking booking = await BookingServices.GetByIdsAsync(CurrentUser.Id, eventId);

            if (@event == null || booking == null || [email protected]() ||
                [email protected]())
            {
                return(BadRequest("Can't unsubscribe to the event"));
            }

            @event.SubscribedNumber--;

            if (@event.ValidationRequired && (bool)booking.Validated)
            {
                @event.ValidatedNumber--;
            }

            try {
                Context.Update(@event);
                Context.Remove(booking);
                await Context.SaveChangesAsync();
            } catch (DbUpdateException e) {
                return(BadRequest(e.InnerException.Message));
            }

            return(NoContent());
        }