public async Task <bool> Add(Draughtsman entity) { await _dbContext.tb_Draughtsman.AddAsync(entity); return(await Save()); }
public async Task <bool> Update(Draughtsman entity) { _dbContext.tb_Draughtsman.Update(entity); return(await Save()); }
public async Task <ActionResult> Create(CreateDraughtsmanViewModel entity) { try { if (_user.FindByNameAsync(entity.ApplicationUser.IdentityUser.UserName).Result == null) { var email = entity.ApplicationUser.IdentityUser.UserName; var user = new IdentityUser { UserName = email, Email = email, EmailConfirmed = true }; var result = _user.CreateAsync(user, entity.Password).Result; var role = _user.AddToRoleAsync(user, "Draughtsman"); } int companyId; var newuser = _user.FindByNameAsync(entity.ApplicationUser.IdentityUser.UserName).Result; var newentity = new ApplicationUser { FirstName = entity.ApplicationUser.FirstName, LastName = entity.ApplicationUser.LastName, Address = entity.ApplicationUser.Address, Id = _user.FindByNameAsync(entity.ApplicationUser.IdentityUser.UserName).Result.Id, }; if (!ModelState.IsValid) { return(View(newentity)); } var applicationUser = _mapper.Map <ApplicationUser>(newentity); //applicationUser.DateCreated = DateTime.Now; var isAppUserSuccess = await _appUserRepo.Add(applicationUser); if (!isAppUserSuccess) { ModelState.AddModelError("", "Something went wrong..."); return(View(entity)); } if (!ModelState.IsValid) { return(View(entity)); } var centity = new CreateCompanyViewModel { CompanyName = entity.Company.CompanyName, CompanyContactNo = entity.Company.CompanyContactNo, CompanyEmail = entity.Company.CompanyEmail }; var newCompany = _mapper.Map <Company>(centity); using (var memoryStream = new MemoryStream()) { await centity.CompanyLogo.CopyToAsync(memoryStream); newCompany.CompanyLogo = memoryStream.ToArray(); } var exists = await _companyRepo.DoesExist(centity.CompanyEmail); if (!exists) { await _companyRepo.Add(newCompany); } if (!ModelState.IsValid) { return(View(entity)); } companyId = await _companyRepo.FindIdByDetails(centity.CompanyName, centity.CompanyEmail); var dentity = new Draughtsman { DraughtsmanRegNo = entity.DraughtsmanRegNo, ApplicationUserId = newentity.ApplicationUserId, CompanyId = companyId }; var draughtsman = _mapper.Map <Draughtsman>(dentity); var isSuccess = await _repo.Add(draughtsman); if (!isSuccess) { ModelState.AddModelError("", "Something went wrong..."); return(View(entity)); } return(RedirectToAction(nameof(Index))); } catch { return(View()); } }