/// <inheritdoc/> public async Task <ProductCategory> UpdateCategoryAsync(int categoryId, UpdateProductCategoryRequest updateRequest) { var dbCategories = await _context.Categories.Where(c => c.Code == updateRequest.Code && c.Id != categoryId).ToArrayAsync(); if (dbCategories.Length > 0) { throw new RequestedResourceHasConflictException("code"); } dbCategories = await _context.Categories.Where(c => c.Id == categoryId).ToArrayAsync(); var dbCategory = dbCategories.FirstOrDefault(); if (dbCategory == null) { throw new RequestedResourceNotFoundException(); } Mapper.Map(updateRequest, dbCategory); dbCategory.LastUpdatedBy = _userContext.UserId; await _context.SaveChangesAsync(); dbCategories = await _context.Categories.Where(c => c.Id == categoryId).ToArrayAsync(); return(dbCategories.Select(c => Mapper.Map <ProductCategory>(c)).FirstOrDefault()); }
/// <inheritdoc/> public async Task <ProductCategory> UpdateCategoryAsync(int categoryId, UpdateProductCategoryRequest updateRequest) { var dbCategory = await _context.Categories .FirstOrDefaultAsync(c => c.Code == updateRequest.Code && c.Id != categoryId) .ConfigureAwait(false); if (dbCategory != null) { throw new RequestedResourceHasConflictException("code"); } dbCategory = await _context.Categories .FirstOrDefaultAsync(c => c.Id == categoryId) .ConfigureAwait(false); if (dbCategory == null) { throw new RequestedResourceNotFoundException(); } Mapper.Map(updateRequest, dbCategory); dbCategory.LastUpdatedBy = _userContext.UserId; await _context.SaveChangesAsync().ConfigureAwait(false); var updateCategory = await GetCategoryAsync(categoryId).ConfigureAwait(false); return(updateCategory); }
/// <inheritdoc/> public async Task <ProductCategory> CreateCategoryAsync(UpdateProductCategoryRequest createRequest) { var dbCategories = await _context.Categories.Where(c => c.Code == createRequest.Code).ToArrayAsync(); if (dbCategories.Length > 0) { throw new RequestedResourceHasConflictException("code"); } var dbCategory = Mapper.Map <UpdateProductCategoryRequest, DbProductCategory>(createRequest); dbCategory.CreatedBy = _userContext.UserId; dbCategory.LastUpdatedBy = _userContext.UserId; _context.Categories.Add(dbCategory); await _context.SaveChangesAsync(); return(Mapper.Map <ProductCategory>(dbCategory)); }
/// <inheritdoc/> public async Task <ProductCategory> CreateCategoryAsync(UpdateProductCategoryRequest createRequest) { var dbCategories = await _context.Categories.FirstOrDefaultAsync(c => c.Code == createRequest.Code).ConfigureAwait(false); if (dbCategories != null) { throw new RequestedResourceHasConflictException(Resources.ProductCategoryCodeError); } var dbCategory = Mapper.Map <UpdateProductCategoryRequest, DbProductCategory>(createRequest); dbCategory.CreatedBy = _userContext.UserId; dbCategory.LastUpdatedBy = _userContext.UserId; _context.Categories.Add(dbCategory); await _context.SaveChangesAsync().ConfigureAwait(false); return(Mapper.Map <ProductCategory>(dbCategory)); }