public async Task <IActionResult> PutResource(int id, Resource resource) { if (id != resource.Id) { return(BadRequest()); } _context.Entry(resource).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ResourceExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutSubCategory(int id, SubCategory subCategory) { if (id != subCategory.Id) { return(BadRequest()); } _context.Entry(subCategory).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SubCategoryExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <int> ChangeInformationAsync(string token, Provider provider) { NullCheck.ThrowIfNull <Provider>(provider); AddressEntity location = new AddressEntity().build(provider.address); _addressMaker.SetCoordinates(location); var query = from o in _context.offer as IQueryable <OfferEntity> join ap in _context.address on o.address_id equals ap.Id where o.token == token select new { o, ap }; foreach (var collection in await query.ToListAsync()) { collection.o.name = provider.name; //o.ispublic = provider.ispublic; //TODO everything non public so far collection.o.organisation = provider.organisation; collection.o.phone = provider.phone; collection.ap.OverwriteWith(location); } var changedRows = await _context.SaveChangesAsync(); if (2 < changedRows) { throw new InvalidDataStateException(FatalCodes.UpdatesMadeInTooManyRows); } return(changedRows); }
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 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("Id,Name")] Person person) { if (ModelState.IsValid) { _context.Add(person); await _context.SaveChangesAsync(); return(RedirectToAction("Index", "ResourceEvents")); } return(View(person)); }
/// <summary> /// Async applying patch. /// </summary> /// <param name="resourceId">id of resource</param> /// <param name="patchDtos">List of PATCHes</param> /// <returns>Modified entity</returns> public async Task ApplyPatchAsync(int resourceId, List <PatchDto> patchDtos) { if (patchDtos == null) { throw new ArgumentNullException(); } using (var resourceContext = new ResourceContext()) { var entity = resourceContext.Resources.FirstOrDefault(x => x.Id == resourceId); if (entity == null) { return; } foreach (var patchDto in patchDtos) { var prop = entity.GetType().GetProperty(patchDto.PropertyName, BindingFlags.Public | BindingFlags.Instance); if (prop != null && prop.CanWrite) { prop.SetValue(entity, Convert.ChangeType(patchDto.PropertyValue, prop.PropertyType)); } } var dbEntityEntry = resourceContext.Entry(entity); dbEntityEntry.State = EntityState.Modified; await resourceContext.SaveChangesAsync(); } }
public async Task <IInsertable> InsertAsync(ResourceContext context) { NullCheck.ThrowIfNull <ResourceContext>(context); context.address.Add(this); await context.SaveChangesAsync(); return(this); }
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 DeleteAsync(ResourceContext context) { NullCheck.ThrowIfNull <ResourceContext>(context); context.address.Remove(this); await context.SaveChangesAsync(); }
public async Task UpdateAsync(ResourceContext context) { NullCheck.ThrowIfNull <ResourceContext>(context); context.consumable.Update(this); await context.SaveChangesAsync(); }
public async Task DeleteAsync(ResourceContext context) { NullCheck.ThrowIfNull <ResourceContext>(context); context.demand_consumable.Remove(this); await context.SaveChangesAsync(); }
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()); }