public IActionResult CreateEvent(EventViewModel vm) { if (!ModelState.IsValid) { return(View(vm)); } if (vm.Date < DateTime.Now) { ModelState.AddModelError("Date", "Data wydarzenia musi być przyszła"); return(View(vm)); } var address = new Address ( vm.City, vm.Street, vm.BuildingNumber, vm.ApartmentNumber, vm.PostalCode ); var existingAddress = _db.Addresses.FirstOrDefault(a => a.City == address.City && a.Street == address.Street && a.BuildingNumber == address.BuildingNumber && a.ApartmentNumber == address.ApartmentNumber && a.PostalCode == address.PostalCode ); if (existingAddress != null) { address.AddressId = existingAddress.AddressId; } var eventToAdd = new Event ( vm.Name, vm.Date, vm.Description, vm.RequiredPoints, vm.Tags, LoggedUser as Organizer, address ); _db.Events.Add(eventToAdd); _db.SaveChanges(); string relativePath = string.Empty; if (vm.FormFile == null) { relativePath = _formFilesManagement.GetPathToRandomStockImage(); } else { relativePath = _formFilesManagement.SaveFileToFileSystem(vm.FormFile, eventToAdd.EventId); } eventToAdd.ImageRelativePath = relativePath; _db.SaveChanges(); return(RedirectToAction("EventsList", "OrganizerPanel")); }
public IActionResult UpdateEvent(EventViewModel vm) { if (!ModelState.IsValid) { return(View(vm)); } if (vm.Date < DateTime.Now) { ModelState.AddModelError("Date", "Data wydarzenia musi być przyszła"); return(View(vm)); } var eventToUpdate = _db.Events.Include(e => e.Address).FirstOrDefault(e => e.EventId == vm.EventId); if (eventToUpdate == null) { return(BadRequest(ErrorMessagesProvider.EventErrors.EventNotExists)); } if (eventToUpdate.Date < DateTime.Now.AddMinutes(1)) { return(BadRequest(ErrorMessagesProvider.EventErrors.EventDatePassed)); } Address address = null; if (!(eventToUpdate.Address.City == vm.City && eventToUpdate.Address.Street == vm.Street && eventToUpdate.Address.BuildingNumber == vm.BuildingNumber && eventToUpdate.Address.ApartmentNumber == vm.ApartmentNumber && eventToUpdate.Address.PostalCode == vm.PostalCode)) { address = new Address ( vm.City, vm.Street, vm.BuildingNumber, vm.ApartmentNumber, vm.PostalCode ); var existingAddress = _db.Addresses.FirstOrDefault(a => a.City == address.City && a.Street == address.Street && a.BuildingNumber == address.BuildingNumber && a.ApartmentNumber == address.ApartmentNumber && a.PostalCode == address.PostalCode ); if (existingAddress != null) { address = existingAddress; } } string relativePath = eventToUpdate.ImageRelativePath; if (vm.FormFile != null) { try { _formFilesManagement.DeleteWholeEventFolder(vm.EventId); } catch (DirectoryNotFoundException) { } // stock files relativePath = _formFilesManagement.SaveFileToFileSystem(vm.FormFile, vm.EventId); } eventToUpdate.UpdateEvent(vm.Name, vm.Date, vm.Description, vm.RequiredPoints, vm.Tags, address, relativePath); _db.Events.Update(eventToUpdate); _db.SaveChanges(); return(RedirectToAction("EventsList")); }