Esempio n. 1
0
        public CRUDResponse PostAsync(EventCreationModel e)
        {
            ApplicationUser CurrentUser = GetCurrentUser();
            Event           newEvent    = null;

            if (e.date < DateTime.Now)
            {
                return new CRUDResponse
                       {
                           IsSuccess = false,
                           Message   = "Date cannot be earlier than today"
                       }
            }
            ;

            try
            {
                if (DbContext.Events.Count() == 0)
                {
                    newEvent = new Event()
                    {
                        EventId     = 1,
                        Description = e.description,
                        Name        = e.name,
                        Subscribers = new List <ApplicationUserEvent>(),
                        Creator     = CurrentUser,
                        Date        = e.date
                    };
                }
                else
                {
                    newEvent = new Event()
                    {
                        EventId     = DbContext.Events.Max(e => e.EventId) + 1,
                        Description = e.description,
                        Name        = e.name,
                        Subscribers = new List <ApplicationUserEvent>(),
                        Creator     = CurrentUser,
                        Date        = e.date
                    };
                }
            }
            catch (Exception ex)
            {
                return(new CRUDResponse
                {
                    IsSuccess = false,
                    Message = ex.Message
                });
            }
            DbContext.Events.Add(newEvent);
            DbContext.SaveChanges();
            return(new CRUDResponse
            {
                IsSuccess = true,
                Message = newEvent.EventId.ToString()
            });
        }
        public async Task <ActionResult <EventModel> > CreateEvent(Guid userId,
                                                                   [FromBody] EventCreationModel eventCreationModel)
        {
            UserServiceModel user = await _userService.GetByPrincipalAsync(User);

            if (user == null || user.Id != userId)
            {
                return(Unauthorized());
            }

            EventCreationServiceModel eventCreationServiceModel = _mapper
                                                                  .Map <EventCreationModel, EventCreationServiceModel>(eventCreationModel);
            EventServiceModel eventServiceModel = await _eventService.AddAsync(eventCreationServiceModel);

            EventModel eventModel = _mapper.Map <EventServiceModel, EventModel>(eventServiceModel);

            return(CreatedAtAction(nameof(GetEvent), new
            {
                eventId = eventModel.Id,
                userId = user.Id
            }, eventModel));
        }
Esempio n. 3
0
 public ActionResult Create(EventCreationModel model)
 {
     if (ModelState.IsValid)
     {
         HttpPostedFileBase file      = Request.Files.Count != 0 ? Request.Files.Get(0) : null;
         TrekEvent          trekEvent = new TrekEvent
         {
             Created = DateTime.Now,
             Name    = model.Name,
             OwnerId = System.Web.HttpContext.Current.User.Identity.GetUserId(),
             Starts  = model.Starts,
             Ends    = model.Starts,
             Route   = model.Route,
             Image   = file != null?ConvertToBytes(file) : null,
                           Description = model.Description,
                           Confirmed   = false
         };
         unitOfWork.TrekEvents.Add(trekEvent);
         unitOfWork.Complete();
         return(RedirectToAction("Index", "Home"));
     }
     return(View(model));
 }