public async Task <Response <EmployeeDto> > Update(Guid id, CreateEmployeeDto entityDto) { Employee employee = _context.Employees .Include(u => u.User) .SingleOrDefault(e => e.Id == id) ?? throw new RestException(HttpStatusCode.NotFound, new { Employee = "Not found" }) ; if (!string.IsNullOrWhiteSpace(entityDto.Password)) { await _userManager.RemovePasswordAsync(employee.User); await _userManager.AddPasswordAsync(employee.User, entityDto.Password); } employee.User.Email = entityDto.Email; employee.User.UserName = entityDto.UserName; employee.User.PhoneNumber = entityDto.Phone; employee.Salary = entityDto.Salary; employee.JoiningDate = entityDto.JoiningDate; employee.Address = entityDto.Address; employee.UpdatedAt = DateTime.Now; if (entityDto.Photo != null) { if (employee.Photo != null) { _photoAccessor.Delete(employee.Photo); } string path = _photoAccessor.Add("employees", entityDto.Photo); employee.Photo = path; } var success = await _context.SaveChangesAsync() > 0; if (success) { var employeeDto = _mapper.Map <Employee, EmployeeDto>(employee); return(new Response <EmployeeDto> { Data = employeeDto, Message = $"Employee {employeeDto.UserName} updated", Time = DateTime.Now, IsSuccess = true }); } ; throw new Exception("Problem on saving employee"); }
public Response <ProductDto> Delete(Guid id) { Product product = _context.Products.Find(id); if (product == null) { throw new RestException(HttpStatusCode.NotFound, new { Product = "Not found" }); } if (product.Photo != null) { _photoAccessor.Delete(product.Photo); } _context.Products.Remove(product); var success = _context.SaveChanges() > 0; if (success) { return(new Response <ProductDto> { Data = null, Message = $"'{product.Name}' removed successfully", Time = DateTime.Now, IsSuccess = true }); } throw new Exception("Problem on deleting product"); }
public async Task <Response <SupplierDto> > Update(Guid id, CreateSupplierDto entityDto) { Supplier supplier = await _context.Suppliers.FindAsync(id); if (supplier == null) { throw new RestException(HttpStatusCode.NotFound, new { Supplier = "Not found" }); } supplier.Address = entityDto.Address; supplier.Email = entityDto.Email; supplier.Name = entityDto.Name; supplier.Phone = entityDto.Phone; supplier.ShopName = entityDto.ShopName; supplier.UpdatedAt = DateTime.Now; if (entityDto.Photo != null) { if (supplier.Photo != null) { _photoAccessor.Delete(supplier.Photo); } string path = _photoAccessor.Add("suppliers", entityDto.Photo); supplier.Photo = path; } var success = await _context.SaveChangesAsync() > 0; if (success) { var supplierDto = _mapper.Map <Supplier, SupplierDto>(supplier); return(new Response <SupplierDto> { Data = supplierDto, Message = $"Supplier {supplierDto.Name} updated", Time = DateTime.Now, IsSuccess = true }); } ; throw new Exception("Problem on saving category"); }