public void CreateAndUpdateOfficer(AddOfficerDTO dto) { if (dto.OfficerId != 0) { var officer = dBContext.Officers.FirstOrDefault(x => x.Id == dto.OfficerId); if (officer != null) { officer.CompanyId = dto.CompanyId; officer.ContactNumber = dto.ContactNumber; officer.Address = dto.Address; officer.EmailAddress = dto.EmailAddress; officer.DepartmentId = dto.DepartmentId; officer.OfficerName = dto.OfficerName; dBContext.SaveChanges(); } } else { var officer = new Officer() { CompanyId = dto.CompanyId, ContactNumber = dto.ContactNumber, Address = dto.Address, EmailAddress = dto.EmailAddress, DepartmentId = dto.DepartmentId, OfficerName = dto.OfficerName }; dBContext.Officers.Add(officer); dBContext.SaveChanges(); } }
public IActionResult CreateOfficer(AddOfficerVM addOfficerVM) { if (!ModelState.IsValid) { ShowToaster("Please fill required fields", ToasterLevel.Danger); return(RedirectToAction("Officers", "Company", new { companyId = addOfficerVM.CompanyId })); } var config = new MapperConfiguration(cfg => cfg.CreateMap <AddOfficerVM, AddOfficerDTO>()); var mapper = new Mapper(config); AddOfficerDTO dto = mapper.DefaultContext.Mapper.Map <AddOfficerDTO>(addOfficerVM); _userService.CreateAndUpdateOfficer(dto); ShowToaster("Officer created successfully", ToasterLevel.Success); return(RedirectToAction("Officers", "Company", new { companyId = dto.CompanyId })); }