public async Task <ActionResult> UpdateAsync(int id, [FromBody] JsonPatchDocument <CompanyInputDto> patchDoc)
        {
            if (id <= 0)
            {
                return(BadRequest());
            }

            var patchTester = new CompanyInputDto();

            patchDoc.ApplyTo(patchTester, ModelState);
            TryValidateModel(patchTester);
            if (!ModelState.IsValid)
            {
                return(ValidationProblem());
            }

            var company = new Company {
                Id = id
            };

            patchDoc.ApplyTo(company);

            if (await CustomerLogic.UpdateCompanyAsync(company))
            {
                return(Ok());
            }

            var errorResult = CheckProblems();

            return((ActionResult)errorResult ?? NoContent());
        }
Exemplo n.º 2
0
        public async Task AddCompany(CompanyInputDto input)
        {
            var company = await db.Companys.SingleOrDefaultAsync(m => m.Name == input.Name && !m.IsDelete);

            if (company != null)
            {
                throw new HttpResponseException(new HttpResponseMessage()
                {
                    Content = new StringContent(JsonConvert.SerializeObject(new ResponseApi()
                    {
                        Code = EExceptionType.Implement, Message = "单位已存在"
                    }))
                });
            }
            else
            {
                db.Companys.Add(new Company()
                {
                    Id       = IdentityManager.NewId(),
                    Name     = input.Name,
                    IsDelete = false
                });

                if (await db.SaveChangesAsync() <= 0)
                {
                    throw new HttpResponseException(new HttpResponseMessage()
                    {
                        Content = new StringContent(JsonConvert.SerializeObject(new ResponseApi()
                        {
                            Code = EExceptionType.Implement, Message = "添加失败"
                        }))
                    });
                }
            }
        }
 private void AddDataAlreadyExistsProblem(CompanyInputDto company)
 {
     _problemCollector.AddProblem(new CodedProblemDetails
                                  (
                                      ProblemType.ERROR_DATA_ALREADY_EXISTS,
                                      $"{nameof(Company)}.{nameof(Company.Npwp)}",
                                      company.Npwp
                                  ));
 }
        public async Task <ActionResult> CreateAsync([FromBody] CompanyInputDto companyInput)
        {
            var company = Mapper.Map <Company>(companyInput);
            await CustomerLogic.CreateCompanyAsync(company);

            var errorResult = CheckProblems();

            return(errorResult ?? CreatedAtAction(
                       Url.Action(nameof(GetAsync)), new { id = company.Id }, company));
        }
Exemplo n.º 5
0
        public bool CheckDuplicateCompany(CompanyInputDto input)
        {
            var data  = false;
            var query = _NewCompanyRepository.GetAll().Where(p => p.Name == input.CompanyName).ToList();

            if (query.Count() > 0)
            {
                data = true;
            }

            return(data);
        }
Exemplo n.º 6
0
        public async Task UpdateCompany(CompanyInputDto input)
        {
            if (await db.Companys.AnyAsync(m => m.Name == input.Name && m.Id != input.CompanyId && !m.IsDelete))
            {
                throw new HttpResponseException(new HttpResponseMessage()
                {
                    Content = new StringContent(JsonConvert.SerializeObject(new ResponseApi()
                    {
                        Code = EExceptionType.Implement, Message = "单位已存在"
                    }))
                });
            }
            else
            {
                var company = await db.Companys.SingleOrDefaultAsync(m => m.Id == input.CompanyId && !m.IsDelete);

                if (company != null)
                {
                    company.Name = input.Name;
                }
                else
                {
                    throw new HttpResponseException(new HttpResponseMessage()
                    {
                        Content = new StringContent(JsonConvert.SerializeObject(new ResponseApi()
                        {
                            Code = EExceptionType.Implement, Message = "该单位不存在"
                        }))
                    });
                }

                if (await db.SaveChangesAsync() <= 0)
                {
                    throw new HttpResponseException(new HttpResponseMessage()
                    {
                        Content = new StringContent(JsonConvert.SerializeObject(new ResponseApi()
                        {
                            Code = EExceptionType.Implement, Message = "修改失败"
                        }))
                    });
                }
            }
        }