public async Task <Result <CompanyDetailDTO> > AddCompanyAsync(CompanyDetailDTO company, PanelUserDTO manager) { Result <CompanyDetailDTO> res = new Result <CompanyDetailDTO>(); _unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted); try { Company entity = Mapper.Map <CompanyDetailDTO, Company>(company); Company created = await _unitOfWork.EntityRepository <Company>().CreateAsync(entity); await _unitOfWork.SaveChangesAsync(); manager.CompanyId = created.Id; manager.Role = UserRole.Admin; if (!string.Equals(company.Demo, "Evet", StringComparison.InvariantCultureIgnoreCase)) { Result <PanelUserDTO> result = await _panelUserService.AddUserAsync(manager); if (!result.IsSuccess) { _unitOfWork.Rollback(); return(res.Fail(result.Messages)); } } _unitOfWork.Commit(); return(Result.Data(Mapper.Map <Company, CompanyDetailDTO>(created))); } catch (Exception) { _unitOfWork.Rollback(); throw; } }
public async Task <IActionResult> Create(PanelUserFormModel model) { try { FillPanelUserFormModel(model); if (!this.ModelState.IsValid) { return(this.View((object)model)); } if (string.IsNullOrWhiteSpace(model.Email)) { model.IsSuccess = false; model.FormMessage = "E-posta adresi gerekmektedir"; return(this.View((object)model)); } if (string.IsNullOrWhiteSpace(model.Name)) { model.IsSuccess = false; model.FormMessage = "Ad Soyad gerekmektedir"; return(this.View((object)model)); } if (model.Role == UserRole.Dealer && !model.PlaceId.HasValue) { model.IsSuccess = false; model.FormMessage = "Şube yetkilisi eklemek için mekan seçmeniz gerekmektedir."; return(this.View((object)model)); } if (model.Role == UserRole.Admin && !model.CompanyId.HasValue) { model.IsSuccess = false; model.FormMessage = "Firma yetkilisi eklemek için mekan seçmeniz gerekmektedir."; return(this.View((object)model)); } var companyId = 0; PanelUserDTO panelUserDTO = new PanelUserDTO { Name = model.Name, Email = model.Email, Role = model.Role, CreatedDate = DateTime.UtcNow, CompanyId = companyId = base.CurrentUser.CompanyId, PlaceId = model.PlaceId }; var misafirIsletmemi = false; if (model.Role == UserRole.Dealer) { Place place = _placeService.GetPlaceQueryable().FirstOrDefault((Place w) => (object)(int?)w.Id == (object)model.PlaceId); if (place != null) { panelUserDTO.CompanyId = place.CompanyId; misafirIsletmemi = string.Equals(place.Guest, "Evet", StringComparison.InvariantCultureIgnoreCase); } } if (model.Role == UserRole.Admin) { panelUserDTO.CompanyId = model.CompanyId.Value; var company = _companyService.GetCompanyQueryable().AsNoTracking().First(x => x.Id == model.CompanyId); misafirIsletmemi = string.Equals(company.Demo, "Evet", StringComparison.InvariantCultureIgnoreCase); } panelUserDTO.SendMail = !misafirIsletmemi; Result <PanelUserDTO> result = await _panelUserService.AddUserAsync(panelUserDTO); model.FormMessage = result.FormMessage; model.IsSuccess = result.IsSuccess; if (model.IsSuccess) { model.FormMessage = "İşleminiz başarılı bir şekilde gerçekleştirildi."; } return(this.View((object)model)); } catch (Exception ex) { LoggerExtensions.LogError(_logger, ex, "Create Error", Array.Empty <object>()); model.IsSuccess = false; model.FormMessage = "İşleminiz gerçekleştirilemedi."; return(this.View((object)model)); } }