public async Task <IActionResult> Create([Bind("Id,ResourceTitle")] Resource resource) { if (ModelState.IsValid) { _context.Add(resource); await _context.SaveChangesAsync(); return(RedirectToAction("Index", "ResourceEvents")); } return(View(resource)); }
public async Task <IActionResult> Create([Bind("ResourceId,PersonId,EventDate")] ResourceEvent resourceEvent) { if (ModelState.IsValid) { _resourceContext.Add(resourceEvent); await _resourceContext.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(resourceEvent)); }
public Resource Create(Resource entity) { if (entity.ResourceId == Guid.Empty) { entity.ResourceId = Guid.NewGuid(); } _context.Add(entity); _context.SaveChanges(); return(entity); }
public async Task <IActionResult> Create([Bind("Id,Name")] Person person) { if (ModelState.IsValid) { _context.Add(person); await _context.SaveChangesAsync(); return(RedirectToAction("Index", "ResourceEvents")); } return(View(person)); }
//Creates a Resource public ApiResponse<Domain.Models.Resource> Create(Domain.Models.Resource Resource) { bool invalidDate = false; try { if (Resource == null) { return new ApiResponse<Domain.Models.Resource>(ApiResponseCode.NoContent, null); } foreach (var timeslot in Resource.TimeSlots) { timeslot.Id = Guid.NewGuid(); } //Checks that the timeslots do not overlap. Resource.TimeSlots.ForEach(delegate (AvailableTime outerTime) { Resource.TimeSlots.ForEach(delegate (AvailableTime innerTime) { if (!(((outerTime.From <= innerTime.From) && (outerTime.To <= innerTime.From)) || ((outerTime.From >= innerTime.To) && (outerTime.To >= innerTime.To)))) { if (outerTime.Id != innerTime.Id) { invalidDate = true; } } }); }); if (!invalidDate) { _applicationContext.Add(Resource); _applicationContext.SaveChanges(); _eventBus.PublishEvent(new ResourceCreatedEvent(Resource)); return new ApiResponse<Domain.Models.Resource>(ApiResponseCode.OK, Resource); } else { return new ApiResponse<Domain.Models.Resource>(ApiResponseCode.NotModified, null); } } catch (Exception e) { return new ApiResponse<Domain.Models.Resource>(ApiResponseCode.InternalServerError, null); } }
public async Task <IActionResult> Create([Bind("Resource, ContactProvided")] ResourceViewModel resourceVM) { try { if (ModelState.IsValid) { var resource = resourceVM.Resource; resource.CategoryID = _uncategorizedId; resource.CreateDate = DateTime.UtcNow; resource.Status = ResourceStatus.New; if (resourceVM.ContactProvided) { // perform some validation ... } else { resource.Contact = null; } _context.Add(resource); await _context.SaveChangesAsync(); //TODO: show a confirmation page upon successful submission. return(RedirectToAction(nameof(CreatedConfirmed))); } } catch (DbUpdateException) { ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } ViewData["CategoryID"] = new SelectList(_context.ResourceCategories, "ID", "CategoryName"); ViewData["TypeID"] = new SelectList(_context.ResourceTypes, "ID", "TypeName"); return(View()); }
public async Task <IActionResult> Create([Bind("Resource, NewCategory, NewTypeName")] ResourceAdminViewModel newResourceVM) { try { if (ModelState.IsValid) { newResourceVM.Resource.CreateDate = DateTime.UtcNow; var newResource = newResourceVM?.Resource; if (newResource == null) { throw new Exception("editedResource creates"); } // TODO: prevent overposting // handle new category var newCategory = newResourceVM.NewCategory; if (!string.IsNullOrEmpty(newCategory.CategoryName)) { if (newCategory.ParentCategoryID != null) { var parentCategory = await _context.ResourceCategories .AsNoTracking() .SingleOrDefaultAsync(c => c.ID == newCategory.ParentCategoryID); if (parentCategory == null) { throw new Exception("Category doesn't exist."); } newCategory.Depth = parentCategory.Depth + 1; } else { newCategory.Depth = 0; } _context.ResourceCategories.Add(newCategory); await _context.SaveChangesAsync(); newResource.CategoryID = newCategory.ID; } // handle new type var newTypeName = newResourceVM.NewTypeName; if (!string.IsNullOrEmpty(newTypeName)) { var newType = new ResourceType { TypeName = newTypeName }; _context.ResourceTypes.Add(newType); await _context.SaveChangesAsync(); newResource.TypeID = newType.ID; } _context.Add(newResource); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } catch (DbUpdateException) { ModelState.AddModelError("", "Unable to save changes. " + "Try again, and if the problem persists " + "see your system administrator."); } ViewData["CategoryDropdownListAddAllowed"] = GetCategorySelectListDFSOrdered(); ViewData["CategoryDropdownList"] = GetCategorySelectListDFSOrdered(currentCategoryId: null, allowAddNew: false); ViewData["TypeDropdownList"] = GetTypeSelectList(); return(View()); }