public async Task <IActionResult> CreateCompanyAsync([FromBody] CompanyModelRq model) { if (!ModelState.IsValid) { Microsoft.AspNetCore.Mvc.ModelBinding.ModelErrorCollection modelErrors = new Microsoft.AspNetCore.Mvc.ModelBinding.ModelErrorCollection(); foreach (var entry in ModelState.Values) { foreach (var error in entry.Errors) { modelErrors.Add(error); } } return(BadRequest(modelErrors)); } var issuer = GetCurrentUserIdentity <int>(); try { if (await _companyService.CheckCompanyExistsAsync()) { return(BadRequest("Company Exists")); } return(Ok(await _companyService.CreateCompanyAsync(model, issuer))); } catch (Exception e) { return(BadRequest(e.Message)); } }
public async Task <IActionResult> UpdateCompanyAsync(int id, [FromBody] CompanyModelRq model) { var issuer = GetCurrentUserIdentity <int>(); try { return(Ok(await _companyService.UpdateCompanyAsync(id, model, issuer))); } catch (Exception e) { return(BadRequest(e.Message)); } }
public async Task <int> CreateCompanyAsync(CompanyModelRq model, UserIdentity <int> issuer) { try { var entity = _mapper.Map <Company>(model); entity.CreateBy(issuer).UpdateBy(issuer); _companyRepository.Create(entity); return(await _uow.SaveChangesAsync() == 1 ? entity.Id : 0); } catch (Exception e) { throw e; } }
public async Task <bool> UpdateCompanyAsync(int id, CompanyModelRq model, UserIdentity <int> issuer) { try { var entity = await _companyRepository.GetEntityByIdAsync(id); if (entity == null) { return(false); } _mapper.Map(model, entity); entity.UpdateBy(issuer); _companyRepository.Update(entity); if (await _uow.SaveChangesAsync() == 1) { return(true); } return(false); } catch (Exception e) { throw e; } }