public async Task <OrganizationDto> CreateEntityAsync(OrganizationRequest request) { var entity = _mapper.Map <OrganizationRequest, Organization>(request); entity.ThemeId = 1; var result = false; entity.ImageURL = await _fileStorageProvider.UploadFileFromStreamAsync( "https://bsawatcherfiles.blob.core.windows.net/watcher/9580e672-01f4-4429-9d04-4f8d1984b25b.png"); var CreatedEntity = await _uow.OrganizationRepository.CreateAsync(entity); result = await _uow.SaveAsync(); if (result) { //id 1 is for admin CreatedEntity.UserOrganizations.Add(new UserOrganization { UserId = entity.CreatedByUserId, OrganizationId = entity.Id, OrganizationRoleId = 1, }); } result &= await _uow.SaveAsync(); var curentUser = await _uow.UsersRepository.GetFirstOrDefaultAsync(x => x.Id == CreatedEntity.CreatedByUserId, include : user => user.Include(x => x.UserOrganizations)); curentUser.LastPickedOrganizationId = CreatedEntity.Id; curentUser.LastPickedOrganization = CreatedEntity; result &= await _uow.SaveAsync(); if (!result) { return(null); } if (entity == null) { return(null); } var dto = _mapper.Map <Organization, OrganizationDto>(entity); return(dto); }
public async Task <UserDto> CreateEntityAsync(UserRegisterRequest request) // TODO: Need refactoring :) { var user = await GetEntityByIdAsync(request.Uid); if (user != null) { return(user); } var entity = _mapper.Map <UserRegisterRequest, User>(request); entity.NotificationSettings = CreateNotificationSetting(); var newPhotoUrl = await _fileStorageProvider.UploadFileFromStreamAsync( string.IsNullOrWhiteSpace(entity.PhotoURL) ? "https://bsawatcherfiles.blob.core.windows.net/watcher/9c2c0291-728d-4e7b-bcb7-3b9432cb8733.png" // Standart photo path : entity.PhotoURL); entity.PhotoURL = newPhotoUrl; var createdUser = await _uow.UsersRepository.CreateAsync(entity); var result = await _uow.SaveAsync(); if (result) { if (request.InvitedOrganizationId == 0) { var defaultOrganization = new Organization { Name = request.CompanyName ?? "Default", IsActive = true, CreatedByUserId = entity.Id, ThemeId = 1, ImageURL = await _fileStorageProvider.UploadFileFromStreamAsync( "https://bsawatcherfiles.blob.core.windows.net/watcher/9580e672-01f4-4429-9d04-4f8d1984b25b.png") }; createdUser.UserOrganizations.Add( new UserOrganization { Organization = defaultOrganization, UserId = createdUser.Id }); createdUser.LastPickedOrganization = defaultOrganization; } else // invited user { var invitedOrganization = await _uow.OrganizationRepository. GetFirstOrDefaultAsync(x => x.Id == request.InvitedOrganizationId, include : org => org.Include(uo => uo.UserOrganizations)); createdUser.UserOrganizations.Add( new UserOrganization { OrganizationId = invitedOrganization.Id, UserId = createdUser.Id }); result &= await _uow.SaveAsync(); createdUser.LastPickedOrganizationId = invitedOrganization.Id; } result &= await _uow.SaveAsync(); } if (!result) { return(null); } var resul = await GetEntityByIdAsync(entity.Id); return(resul); }