Ejemplo n.º 1
0
        public async Task <ActionResult> UpdateAsync([FromBody] PartDTO part, CancellationToken cancellationToken)
        {
            InitUserInfo();
            if (!AllowUpdate)
            {
                return(ValidationProblem());
            }
            var specFilter = new PartFilterSpecification(part.Id, true);
            var rowCount   = await _partService.CountAsync(specFilter, cancellationToken);

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

            // bind to old item


            var objItem = _mapper.Map <Part>(part);

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

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

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

            return(CreatedAtAction(nameof(GetIdAsync), new { id = objItem.Id }, null));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> UpdateDraftAsync(string id, [FromBody] PartDTO partDto, CancellationToken cancellationToken = default)
        {
            InitUserInfo();
            if (!AllowCreate && !AllowUpdate)
            {
                return(ValidationProblem());
            }
            var specFilter = new PartFilterSpecification(partDto.Id, true);
            var rowCount   = await _partService.CountAsync(specFilter, cancellationToken);

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

            var part   = _mapper.Map <Part>(partDto);
            var result = await _partService.PatchDraft(part, cancellationToken);

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

            return(CreatedAtAction(nameof(GetIdAsync), new { id = id }, null));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> CreateAsync([FromBody] PartDTO part, CancellationToken cancellationToken)
        {
            InitUserInfo();
            if (!AllowCreate)
            {
                return(ValidationProblem());
            }
            // remove temporary id (if any)

            var newItem = _mapper.Map <Part>(part);

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

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

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

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