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

            if (@event == null)
            {
                return(NotFound("Event not found"));
            }
            else if (Context.Tops.Any <TopEvents>((TopEvents arg) => arg.EventId == eventId))
            {
                return(BadRequest("Event has been already add to the top list"));
            }
            else if (@event.Status == Status.DONE)
            {
                return(BadRequest("Event is already done"));
            }

            TopEvents newTopEvent = new TopEvents()
            {
                Event   = @event,
                EventId = @event.Id,
                Index   = Context.Tops.Count() + 1,
                Name    = name
            };

            @event.TopEvents = newTopEvent;

            try {
                Context.Tops.Add(newTopEvent);
                Context.Event.Update(@event);
                Context.SaveChanges();
            } catch (DbUpdateException e) {
                return(BadRequest(e.InnerException.Message));
            }
            return(NoContent());
        }
コード例 #2
0
        public async Task <IActionResult> Subscribe(int eventId)
        {
            Event @event = await EventServices.GetEventByIdAsync(eventId);

            if (@event == null || @event.SubscribedNumber >= @event.SubscribeNumber)
            {
                return(BadRequest("Unprocessable, Max Subscriber reach or event not found"));
            }

            // Don't force lambda to request Db for other scops.
            int userId  = CurrentUser.Id;
            int roleId  = CurrentUser.RoleId;
            int eventID = @event.Id;

            if (Context.Booking.Any((Booking booking) => booking.EventId == eventId &&
                                    booking.UserId == userId))
            {
                return(BadRequest("User Already Booked"));
            }

            if ([email protected]() && [email protected]())
            {
                return(BadRequest("Event Expired, can't subscribe"));
            }

            if ([email protected]())
            {
                return(BadRequest("Subscriptions are not open"));
            }

            if (@event.RestrictedEvent &&
                !Context.EventRole.Any((EventRole ev) => ev.RoleId == roleId && ev.EventId == eventID))
            {
                return(BadRequest("Sorry you are not allowed to subscribe"));
            }

            Booking book = new Booking()
            {
                Present = false,
                User    = CurrentUser,
                Event   = @event
            };

            @event.SubscribedNumber++;
            book.Validated &= [email protected];

            try {
                Context.Booking.Add(book);
                Context.Event.Update(@event);
                await Context.SaveChangesAsync();

                BookingTemplate template;
                if (!book.Validated.HasValue)
                {
                    template = BookingTemplate.AUTO_VALIDATED;
                }
                else
                {
                    template = BookingTemplate.PENDING_VALIDATION;
                }
                EmailServices.SendFor(CurrentUser, @event, template);
            } catch (DbUpdateException e) {
                return(BadRequest(e.InnerException.Message));
            }

            return(NoContent());
        }