public async Task <IActionResult> CreateWarehouseTransaction([FromBody] CreateWarehouseTransactionDto dto)
        {
            var res = await _service.CreateWarehouseTransactionAsync(dto);

            if (res.Success)
            {
                return(Ok(res));
            }

            return(BadRequest(res));
        }
Exemple #2
0
        public async Task <ServiceResponse <GetWarehouseTransactionDto> > CreateWarehouseTransactionAsync(CreateWarehouseTransactionDto dto)
        {
            var response = new ServiceResponse <GetWarehouseTransactionDto>();

            try
            {
                var userIdentifier = _httpCtxAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
                var user           = await _context.Users.SingleOrDefaultAsync(x => x.Email == userIdentifier);

                if (user == null)
                {
                    throw new UserNotFoundException();
                }

                Manufacturer dbManufacturer = null;

                if (dto.TransactionType == WarehouseTransactionType.Import)
                {
                    dbManufacturer = await _context.Manufacturers.FirstOrDefaultAsync(x => x.Id == dto.ManufacturerId);

                    if (dbManufacturer == null)
                    {
                        throw new ManufacturerNotFoundException();
                    }
                }

                var newTransaction = new WarehouseTransaction()
                {
                    TransactionType = dto.TransactionType,
                    CreatedAt       = dto.CreatedAt,
                    Status          = WarehouseTransactionStatus.Processing, // new warehouse transaction has processing status as default
                    Manufacturer    = dbManufacturer,
                    CreatedBy       = user,
                    Description     = dto.Description,
                };

                await _context.WarehouseTransactions.AddAsync(newTransaction);

                var dbWarehouseTransactionItems = dto.WarehouseTransactionItems.Select(item => new WarehouseTransactionItem
                {
                    WarehouseTransaction = newTransaction,
                    ProductId            = item.ProductId,
                    Cost     = item.Cost,
                    Quantity = item.Quantity
                });


                await _context.WarehouseTransactionItems.AddRangeAsync(dbWarehouseTransactionItems);

                await _context.SaveChangeWithValidationAsync();

                response.Data = _mapper.Map <GetWarehouseTransactionDto>(newTransaction);

                return(response);
            }
            catch (BaseServiceException ex)
            {
                response.Success = false;
                response.Message = ex.ErrorMessage;
                response.Code    = ex.Code;

                _logger.LogError(ex.Message, ex.StackTrace);
                return(response);
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
                response.Code    = ErrorCode.WAREHOUSE_TRANSACTION_UNEXPECTED_ERROR;

                _logger.LogError(ex.Message, ex.StackTrace);
                return(response);
            }
        }