public async Task <IActionResult> CreateSupplier(SupplierForCreationDto creationDto) { var accountId = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value; var result = await _supplierService.CreateSupplierAsync(creationDto, accountId); return(StatusCode((int)result.Code, result)); }
public async Task CreateAsync(SupplierForCreationDto element) { var supplier = new Supplier(element.Name, element.Details); _context.Suppliers.Add(supplier); await _context.SaveChangesAsync(); }
public async Task <ApiResult <string> > CreateSupplierAsync(SupplierForCreationDto creationDto, string accountId) { try { var checkEmployee = await _context.Employees.Where(x => x.AppuserId.ToString() == accountId) .SingleOrDefaultAsync(); if (checkEmployee == null) { return(new ApiResult <string>(HttpStatusCode.NotFound, $"Lỗi tài khoản đăng nhập")); } var sequenceNumber = await _context.Suppliers.CountAsync(); var generateId = IdentifyGenerator.GenerateSupplierId(sequenceNumber + 1); var supplier = ObjectMapper.Mapper.Map <SupplierForCreationDto, Data.Entities.Supplier>(creationDto); supplier.Id = generateId; supplier.EmployeeId = checkEmployee.Id; await _context.Suppliers.AddAsync(supplier); await _context.SaveChangesAsync(); return(new ApiResult <string>(HttpStatusCode.OK) { ResultObj = generateId, Message = "Tạo mới nhà cung cấp thành công" }); } catch { _logger.LogInfo("Có lỗi khi thêm nhà cung cấp"); return(new ApiResult <string>(HttpStatusCode.InternalServerError, $"Có lỗi khi tạo mới nhà cung cấp")); } }
public IActionResult CreateSupplier([FromBody] SupplierForCreationDto supplier) { try { if (supplier == null) { _logger.LogError("Supplier object sent from client is null."); return(BadRequest("Supplier object is null")); } if (!ModelState.IsValid) { _logger.LogError("Invalid supplier object sent from client."); return(BadRequest("Invalid model object")); } var supplierEntity = _mapper.Map <Supplier>(supplier); _repository.Supplier.CreateSupplier(supplierEntity); _repository.Save(); var createdSupplier = _mapper.Map <SupplierDto>(supplierEntity); return(CreatedAtRoute("SupplierById", new { id = createdSupplier.SupplierId }, createdSupplier)); } catch (Exception ex) { _logger.LogError($"Something went wrong inside CreateSupplier action: {ex.InnerException.Message}"); return(StatusCode(500, "Internal server error")); } }
public async Task <IActionResult> AddSupplier(SupplierForCreationDto supplierForCreationDto) { var supplier = _mapper.Map <Supplier>(supplierForCreationDto); _repo.Add(supplier); if (await _repo.SaveAll()) { return(CreatedAtRoute("GetSupplier", new { controller = "Suppliers", id = supplier.Id }, supplier)); } throw new Exception($"Adding supplier failed on save"); }
public async Task <IActionResult> PostAsync([FromBody] SupplierForCreationDto value) { await _supplierService.CreateAsync(value); return(Ok()); }