public async Task <IHttpActionResult> AddRequaredInventory([FromUri] int id, [FromBody] UpdateRequiredInventoryRequest createRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var requaredInventory = await _requaredInventoryService.CreateRequaredInventoryAsync(id, createRequest); var location = string.Format("/api/item/{0}", requaredInventory.Id); return(Created <RequiredInventoryItem>(location, requaredInventory)); }
/// <inheritdoc/> public async Task <RequiredInventoryItem> CreateRequaredInventoryAsync(UpdateRequiredInventoryRequest createRequest) { var dbRequaredInventory = Mapper.Map <UpdateRequiredInventoryRequest, RequiredInventory>(createRequest); int chapterNumber = _context.RequiredInventories .Where(r => r.Path.Length == 3) .Max(r => r.Number) + 1; string path = chapterNumber.ToString().PadLeft(3, '0'); dbRequaredInventory.Number = chapterNumber; dbRequaredInventory.Path = path; _context.RequiredInventories.Add(dbRequaredInventory); await _context.SaveChangesAsync(); return(Mapper.Map <RequiredInventoryItem>(dbRequaredInventory)); }
/// <inheritdoc/> public async Task <RequiredInventoryItem> CreateRequaredInventoryAsync(int parentId, UpdateRequiredInventoryRequest createRequest) { var dbRequaredInventory = Mapper.Map <UpdateRequiredInventoryRequest, RequiredInventory>(createRequest); var dbRequaredInventoryParent = await _context.RequiredInventories.Where(o => o.Id == parentId).ToArrayAsync(); if (dbRequaredInventoryParent.Length == 0) { throw new RequestedResourceNotFoundException(); } string parentPath = dbRequaredInventoryParent[0].Path + '.'; var dbRequaredInventoryChild = await _context.RequiredInventories .Where(r => r.Path.StartsWith(parentPath) && r.Path.Length == parentPath.Length + 3) .ToArrayAsync(); int chapterNumber = dbRequaredInventoryChild.Length > 0 ? dbRequaredInventoryChild.Max(r => r.Number) + 1 : 1; string path = parentPath + chapterNumber.ToString().PadLeft(3, '0'); dbRequaredInventory.Number = chapterNumber; dbRequaredInventory.Path = path; _context.RequiredInventories.Add(dbRequaredInventory); await _context.SaveChangesAsync(); return(Mapper.Map <RequiredInventoryItem>(dbRequaredInventory)); }
/// <inheritdoc/> public async Task <RequiredInventoryItem> UpdateRequaredInventoryAsync(int requaredInventoryId, UpdateRequiredInventoryRequest updateRequest) { var dbRequaredInventorys = await _context.RequiredInventories.Where(p => p.Id == requaredInventoryId).ToArrayAsync(); if (dbRequaredInventorys.Length == 0) { throw new RequestedResourceNotFoundException(); } var dbRequaredInventory = dbRequaredInventorys[0]; Mapper.Map(updateRequest, dbRequaredInventory); await _context.SaveChangesAsync(); return(Mapper.Map <RequiredInventoryItem>(dbRequaredInventory)); }
public async Task <IHttpActionResult> UpdateRequaredInventory([FromUri] int id, [FromBody] UpdateRequiredInventoryRequest updateRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } await _requaredInventoryService.UpdateRequaredInventoryAsync(id, updateRequest); return(ResponseMessage(Request.CreateResponse(HttpStatusCode.NoContent))); }