public async Task <IActionResult> Create([FromBody] CreateEventViewModel model) { var category = await _db.Categories.FirstOrDefaultAsync(x => x.Name == model.Category); var appUser = await _db.Users.FirstOrDefaultAsync(x => x.Email == User.Identity.Name); var newEvent = new Event { Name = model.Name, Slug = model.Name.GenerateSlug(), DateCreated = DateTime.Now, DateStarts = model.DateStarts, DateEnds = model.DateEnds, PeopleNeeded = model.PeopleNeeded, City = model.City, Street = model.Street, BuildingNumber = model.BuildingNumber, ObjectName = model.ObjectName, PlaceDescription = model.PlaceDescription, EventDescription = model.EventDescription, WebPage = model.WebPage, IsCancelled = false, Category = category, CreatedBy = appUser.Id, CategoryId = category.Id }; _db.Events.Add(newEvent); await _db.SaveChangesAsync(); return(Ok()); }
public async Task <IActionResult> Update(int id, [FromBody] CreateEventViewModel model) { var category = await _db.Categories.FirstOrDefaultAsync(x => x.Name == model.Category); var appUser = await _db.Users.FirstOrDefaultAsync(x => x.Email == User.Identity.Name); var currentEvent = await _db.Events.FirstOrDefaultAsync(x => x.Id == id); if (currentEvent != null && currentEvent.DateStarts >= DateTime.Now) { currentEvent.Name = model.Name; currentEvent.DateStarts = model.DateStarts; currentEvent.DateEnds = model.DateEnds; currentEvent.PeopleNeeded = model.PeopleNeeded; currentEvent.City = model.City; currentEvent.Street = model.Street; currentEvent.BuildingNumber = model.BuildingNumber; currentEvent.ObjectName = model.ObjectName; currentEvent.PlaceDescription = model.PlaceDescription; currentEvent.EventDescription = model.EventDescription; currentEvent.WebPage = model.WebPage; currentEvent.Category = category; currentEvent.CategoryId = category.Id; _db.Events.Update(currentEvent); await _db.SaveChangesAsync(); return(Ok()); } else { return(Forbid()); } }