public async Task DeleteRights(int collaboratorId, int projectId, int currentUserId) { var project = await _context.Projects.Where(item => item.Id == projectId).FirstOrDefaultAsync(); if (project.AuthorId == currentUserId) { var collaborator = await _context.ProjectMembers .Where(item => item.UserId == collaboratorId && item.ProjectId == projectId) .FirstOrDefaultAsync(); _context.ProjectMembers.Remove(collaborator); await _context.SaveChangesAsync(); } else { _logger.LogWarning(LoggingEvents.HaveException, $"NonAuthorRightsChange"); throw new NonAuthorRightsChange(); } var notification = new NotificationDTO() { Type = NotificationType.AssinedToProject, ProjectId = projectId, DateTime = DateTime.Now, Message = $"You was deleted from project \"{project.Name}\".", Status = NotificationStatus.Message }; using (var scope = _serviceScopeFactory.CreateScope()) { var notificationService = scope.ServiceProvider.GetService <INotificationService>(); await notificationService.SendNotificationToUserById(collaboratorId, notification); } }
public async Task <int> CreateProject(ProjectCreateDTO projectCreateDto, int userId) { var project = _mapper.Map <Project>(projectCreateDto); var user = await _userService.GetUserDetailsById(userId); project.AuthorId = userId; project.CreatedAt = DateTime.Now; project.AccessModifier = projectCreateDto.Access; var userEditorSettings = (await _userService.GetUserDetailsById(userId)).EditorSettings; var newProjectEditorSetting = new EditorSettingDTO { CursorStyle = userEditorSettings.CursorStyle, FontSize = userEditorSettings.FontSize, ScrollBeyondLastLine = userEditorSettings.ScrollBeyondLastLine, RoundedSelection = userEditorSettings.RoundedSelection, TabSize = userEditorSettings.TabSize, LineHeight = userEditorSettings.LineHeight, LineNumbers = userEditorSettings.LineNumbers, ReadOnly = userEditorSettings.ReadOnly, Theme = userEditorSettings.Theme, Language = (projectCreateDto.Language.ToString()).ToLower() }; var createDTO = await _editorSettingService.CreateEditorSettings(newProjectEditorSetting); project.EditorProjectSettingsId = _mapper.Map <EditorSetting>(createDTO).Id; _context.Projects.Add(project); await _context.SaveChangesAsync(); _logger.LogInformation($"project created {project.Name}"); return(project.Id); }
public async Task <User> CreateUser(UserRegisterDTO userDto) { var userEntity = _mapper.Map <User>(userDto); var user = await _context.Users.Where(a => a.Email == userEntity.Email).FirstOrDefaultAsync(); if (user != null) { _logger.LogWarning(LoggingEvents.HaveException, $"user already exists"); throw new ExistedUserLoginException(); } var salt = SecurityHelper.GetRandomBytes(); userEntity.PasswordSalt = Convert.ToBase64String(salt); userEntity.PasswordHash = SecurityHelper.HashPassword(userDto.Password, salt); userEntity.RegisteredAt = DateTime.Now; userEntity.EditorSettingsId = await _editorSettingService.CreateInitEditorSettings(); if (!string.IsNullOrEmpty(userDto.Picture)) { userEntity.Avatar = new Image { Url = userDto.Picture }; } _context.Users.Add(userEntity); await _context.SaveChangesAsync(); await SendConfirmationMail(userEntity.Id); return(userEntity); }
public async Task <Build> CreateStartBuildResult(int projectId, int userId) { var buildDTO = new BuildDTO(); buildDTO.BuildStarted = DateTime.Now; buildDTO.UserId = userId; buildDTO.ProjectId = projectId; var build = _mapper.Map <Build>(buildDTO); var coutOfExistBuild = _context.Builds .Where(item => item.ProjectId == projectId) .Count(); var project = await _context.Projects .FirstOrDefaultAsync(item => item.Id == projectId); if (coutOfExistBuild == project.CountOfSaveBuilds) { var olderBuild = await _context.Builds .Where(item => item.ProjectId == build.ProjectId) .OrderBy(item => item.BuildStarted) .FirstOrDefaultAsync(); _context.Remove(olderBuild); } await _context.Builds.AddAsync(build); await _context.SaveChangesAsync(); return(build); }
public async Task MarkAsRead(int notificationId) { var notification = await _context.Notifications .FirstOrDefaultAsync(n => n.Id == notificationId) .ConfigureAwait(false); notification.IsRead = true; _context.Update(notification); await _context.SaveChangesAsync() .ConfigureAwait(false); _logger.LogInformation($"notification {notificationId} marked as read"); }
public async Task <AccessTokenDTO> GenerateAccessToken(User user) { var refreshToken = JWTFactory.GenerateRefreshToken(); _context.RefreshTokens.Add(new RefreshToken { Token = refreshToken, UserId = user.Id }); await _context.SaveChangesAsync(); var accessToken = await _jwtFactory.GenerateAccessToken(user); return(new AccessTokenDTO(accessToken, refreshToken)); }
public async Task <EditorSettingDTO> CreateEditorSettings(EditorSettingDTO editorSettinsCreateDto) { var editorSettins = _mapper.Map <EditorSetting>(editorSettinsCreateDto); await _context.EditorSettings.AddAsync(editorSettins); await _context.SaveChangesAsync(); editorSettinsCreateDto = _mapper.Map <EditorSettingDTO>(editorSettins); return(editorSettinsCreateDto); }
public async Task SetFavouriteProject(int projectId, int userId) { var contextFP = _context.FavouriteProjects.FirstOrDefault(x => x.UserId == userId && x.ProjectId == projectId); if (contextFP != null) { _context.FavouriteProjects.Remove(contextFP); } else { var fp = new FavouriteProjects { ProjectId = projectId, UserId = userId }; _context.FavouriteProjects.Add(fp); } await _context.SaveChangesAsync(); }
public async Task <int> AddGitCredentialsToProject(GitCredentialsDTO gitCredentialsDTO, int authorId) { var project = await _context.Projects .SingleOrDefaultAsync(p => p.Id == Convert.ToInt32(gitCredentialsDTO.ProjectId)); var gitCredentials = new GitCredential { Login = gitCredentialsDTO.Login, Url = gitCredentialsDTO.Url, Provider = gitCredentialsDTO.Provider, PasswordHash = gitCredentialsDTO.Password }; project.GitCredential = gitCredentials; project.ProjectLink = gitCredentials.Url.Substring(0, gitCredentials.Url.LastIndexOf('.')); await _context.GitCredentials.AddAsync(gitCredentials); await _context.SaveChangesAsync(); await Clone(gitCredentialsDTO.ProjectId, project.ProjectLink, authorId); return(gitCredentials.Id); }