Пример #1
0
        public async Task <ActionResult> UpdateDraftAsync(int id, [FromBody] PurchaseOrderDTO purchaseOrderDto, CancellationToken cancellationToken = default)
        {
            InitUserInfo();
            if (!AllowCreate && !AllowUpdate)
            {
                return(ValidationProblem());
            }
            var specFilter = new PurchaseOrderFilterSpecification(int.Parse(purchaseOrderDto.Id), true);
            var rowCount   = await _purchaseOrderService.CountAsync(specFilter, cancellationToken);

            if (rowCount == 0)
            {
                throw new EntityNotFoundException(nameof(PurchaseOrder), purchaseOrderDto.Id);
            }

            var purchaseOrder = _mapper.Map <PurchaseOrder>(purchaseOrderDto);
            var result        = await _purchaseOrderService.PatchDraft(purchaseOrder, cancellationToken);

            if (!result)
            {
                AssignToModelState(_purchaseOrderService.Errors);
                return(ValidationProblem());
            }

            return(CreatedAtAction(nameof(GetIdAsync), new { id = id }, null));
        }
Пример #2
0
        public async Task <ActionResult> CreateAsync([FromBody] PurchaseOrderDTO purchaseOrder, CancellationToken cancellationToken)
        {
            InitUserInfo();
            if (!AllowCreate)
            {
                return(ValidationProblem());
            }
            // remove temporary id (if any)
            foreach (var item in purchaseOrder.PurchaseOrderDetails)
            {
                if (!int.TryParse(item.Id, out _))
                {
                    item.Id = "0";
                }
                if (!int.TryParse(item.PurchaseOrderId, out _))
                {
                    item.PurchaseOrderId = "0";
                }
            }

            var newItem = _mapper.Map <PurchaseOrder>(purchaseOrder);

            // untuk data yang mereference object, perlu di set null agar tidak insert sebagai data baru
            CleanReferenceObject(newItem);

            newItem = await _purchaseOrderService.AddAsync(newItem, cancellationToken);

            if (newItem == null)
            {
                AssignToModelState(_purchaseOrderService.Errors);
                return(ValidationProblem());
            }

            return(CreatedAtAction(nameof(GetIdAsync), new { id = newItem.Id }, null));
        }
Пример #3
0
        public async Task <ActionResult> UpdateAsync([FromBody] PurchaseOrderDTO purchaseOrder, CancellationToken cancellationToken)
        {
            InitUserInfo();
            if (!AllowUpdate)
            {
                return(ValidationProblem());
            }
            var specFilter = new PurchaseOrderFilterSpecification(int.Parse(purchaseOrder.Id), true);
            var rowCount   = await _purchaseOrderService.CountAsync(specFilter, cancellationToken);

            if (rowCount == 0)
            {
                throw new EntityNotFoundException(nameof(PurchaseOrder), purchaseOrder.Id);
            }

            // bind to old item
            foreach (var item in purchaseOrder.PurchaseOrderDetails)
            {
                if (!int.TryParse(item.Id, out _))
                {
                    item.Id = "0";
                }
                if (!int.TryParse(item.PurchaseOrderId, out _))
                {
                    item.PurchaseOrderId = "0";
                }
            }


            var objItem = _mapper.Map <PurchaseOrder>(purchaseOrder);

            // untuk data yang mereference object, perlu di set null agar tidak insert sebagai data baru
            CleanReferenceObject(objItem);

            var result = await _purchaseOrderService.UpdateAsync(objItem, cancellationToken);

            if (!result)
            {
                AssignToModelState(_purchaseOrderService.Errors);
                return(ValidationProblem());
            }

            return(CreatedAtAction(nameof(GetIdAsync), new { id = objItem.Id }, null));
        }