public async Task <IActionResult> MarkSeen([FromBody] LastSeenCreateDto document) { if (document == null) { return(BadRequest()); } var dbDocument = await _db.Documents.SingleOrDefaultAsync(d => d.DocumentId == document.DocumentId); if (dbDocument == null) { return(NotFound()); } var newLastSeen = new LastSeenDocuments { UserId = UserId, DocumentId = document.DocumentId, TimeCreated = DateTime.UtcNow }; _db.LastSeenDocuments.Add(newLastSeen); await _db.SaveChangesAsync(); await _db.Entry(newLastSeen).Reference(l => l.Document).LoadAsync(); return(Ok(new LastSeenDto(newLastSeen))); }
public async Task <IActionResult> CreateRole([FromBody] RoleDto role) { if (role == null) { return(BadRequest()); } var newRole = new Roles { Name = role.Name, System = false }; _db.Roles.Add(newRole); await _db.SaveChangesAsync(); if (role.Permissions != null) { var permissionSlugs = role.Permissions.Select(p => p.PermissionSlug); var existingPermissions = await _db.Permissions.Where(p => permissionSlugs.Contains(p.PermissionSlug)).ToListAsync(); _db.RolePermissions.AddRange(existingPermissions.Select(p => new RolePermissions { RoleId = newRole.RoleId, PermissionSlug = p.PermissionSlug })); await _db.SaveChangesAsync(); } return(Ok(new RoleDto(newRole))); }
public async Task <IActionResult> Follow([FromBody] FollowingCreateDto following) { if (following == null) { return(BadRequest()); } var project = await _db.Projects.SingleOrDefaultAsync(p => p.ProjectId == following.ProjectId); if (project == null) { return(NotFound()); } var existingFollowing = await _db.FollowedProjects.Include(f => f.Project).SingleOrDefaultAsync(f => f.ProjectId == following.ProjectId && f.UserId == UserId); if (existingFollowing != null) { return(Ok(new FollowingDto(existingFollowing))); } var newFollowing = new FollowedProjects { UserId = UserId, ProjectId = following.ProjectId, TimeCreated = DateTime.UtcNow }; _db.FollowedProjects.Add(newFollowing); await _db.SaveChangesAsync(); await _db.Entry(newFollowing).Reference(f => f.Project).LoadAsync(); return(Ok(new FollowingDto(newFollowing))); }
public async Task <IActionResult> Create([FromBody] BookmarkCreateDto bookmark) { if (bookmark == null) { return(BadRequest()); } var document = await _db.Documents.SingleOrDefaultAsync(d => d.DocumentId == bookmark.DocumentId); if (document == null) { return(NotFound()); } var existingBookmark = await _db.Bookmarks.Include(b => b.Document).SingleOrDefaultAsync(b => b.DocumentId == bookmark.DocumentId && b.UserId == UserId); if (existingBookmark != null) { return(Ok(new BookmarkDto(existingBookmark))); } var newBookmark = new Bookmarks { UserId = UserId, DocumentId = bookmark.DocumentId, TimeCreated = DateTime.UtcNow }; _db.Bookmarks.Add(newBookmark); await _db.SaveChangesAsync(); await _db.Entry(newBookmark).Reference(b => b.Document).LoadAsync(); return(Ok(new BookmarkDto(newBookmark))); }
public async Task <IActionResult> Create([FromBody] CompetenceCreateDto competence) { if (competence == null) { return(BadRequest()); } var newCompetence = new Competences { Name = competence.Name }; _db.Competences.Add(newCompetence); await _db.SaveChangesAsync(); return(Ok(new CompetenceDto(newCompetence))); }
public async Task <IActionResult> SetRead() { var currentReadMark = await _db.NotificationsRead.SingleOrDefaultAsync(n => n.UserId == UserId); if (currentReadMark == null) { _db.NotificationsRead.Add(new NotificationsRead { UserId = UserId, TimeRead = DateTime.UtcNow }); } else { currentReadMark.TimeRead = DateTime.UtcNow; } await _db.SaveChangesAsync(); return(Ok()); }
private static async Task SeedProjectRoles(KMSDBContext db, string dirname, JsonSerializerSettings serializerSettings) { var rolesJson = await File.ReadAllTextAsync(Path.Combine(dirname, "project_roles.json")); var roles = JsonConvert.DeserializeObject <List <RoleSerializer> >(rolesJson, serializerSettings); foreach (var role in roles) { var dbRole = await db.ProjectRoles.SingleOrDefaultAsync(r => r.Name == role.Name && r.System == true); if (dbRole == null) { dbRole = new ProjectRoles { Name = role.Name, System = true }; db.ProjectRoles.Add(dbRole); await db.SaveChangesAsync(); } foreach (var permission in role.Permissions) { var dbPermission = await db.ProjectPermissions.SingleOrDefaultAsync(p => p.ProjectPermissionSlug == permission); var dbRolePermission = await db.ProjectRolePermissions.SingleOrDefaultAsync(rp => rp.ProjectRoleId == dbRole.ProjectRoleId && rp.ProjectPermissionSlug == permission); if (dbPermission == null) { throw new Exception("Permission for role not found"); } if (dbRolePermission == null) { db.ProjectRolePermissions.Add(new ProjectRolePermissions { ProjectRoleId = dbRole.ProjectRoleId, ProjectPermissionSlug = permission }); } } await db.SaveChangesAsync(); } }
private static async Task SeedTemplates(KMSDBContext db, string dirname, JsonSerializerSettings serializerSettings) { var templatesJson = await File.ReadAllTextAsync(Path.Combine(dirname, "templates.json")); var templates = JsonConvert.DeserializeObject <List <TemplateSerializer> >(templatesJson, serializerSettings); foreach (var item in templates) { var existingType = await db.TemplateTypes.SingleOrDefaultAsync(t => t.TemplateTypeSlug == item.TemplateTypeSlug); if (existingType == null) { throw new Exception("Template type not found"); } var existingTemplate = await db.Templates.SingleOrDefaultAsync(t => t.TemplateTypeSlug == item.TemplateTypeSlug && t.ProjectId == null); if (existingTemplate == null) { var newTemplate = new Templates { TemplateTypeSlug = item.TemplateTypeSlug, Slug = item.Title.GenerateSlug(), Title = item.Title, Description = item.Description, DateCreated = DateTime.UtcNow }; db.Templates.Add(newTemplate); await db.SaveChangesAsync(); var newTemplateText = new TemplateText { TemplateId = newTemplate.TemplateId, Content = item.Content, TimeUpdated = DateTime.UtcNow }; db.TemplateText.Add(newTemplateText); await db.SaveChangesAsync(); } } }
private static async Task SeedTemplateTypes(KMSDBContext db, string dirname, JsonSerializerSettings serializerSettings) { var templateTypesJson = await File.ReadAllTextAsync(Path.Combine(dirname, "template_types.json")); var templateTypes = JsonConvert.DeserializeObject <List <TemplateTypes> >(templateTypesJson, serializerSettings); foreach (var item in templateTypes) { var existingType = await db.TemplateTypes.SingleOrDefaultAsync(t => t.TemplateTypeSlug == item.TemplateTypeSlug); if (existingType == null) { db.TemplateTypes.Add(item); } } await db.SaveChangesAsync(); }
private static async Task SeedProjectPermissions(KMSDBContext db, string dirname, JsonSerializerSettings serializerSettings) { var permissionsJson = await File.ReadAllTextAsync(Path.Combine(dirname, "project_permissions.json")); var permissions = JsonConvert.DeserializeObject <List <ProjectPermissions> >(permissionsJson, serializerSettings); foreach (var item in permissions) { var existingPermission = await db.ProjectPermissions.SingleOrDefaultAsync(p => p.ProjectPermissionSlug == item.ProjectPermissionSlug); if (existingPermission == null) { db.ProjectPermissions.Add(item); } } await db.SaveChangesAsync(); }
public async Task <IActionResult> Create([FromBody] TemplateCreateDto template) { if (template == null) { return(BadRequest()); } int attemptsCount = 0; int maxAttemptsCount = 100; string slug; bool isSlugUnique = false; do { slug = template.Title.GenerateSlug(); isSlugUnique = (await _db.Templates.SingleOrDefaultAsync(d => d.Slug == slug)) == null; } while (!isSlugUnique && attemptsCount < maxAttemptsCount); if (!isSlugUnique) { return(BadRequest(new { message = "Cannot generate unique slug" })); } var newTemplate = new Templates { ProjectId = template.ProjectId, TemplateTypeSlug = template.TemplateType, CreatorId = UserId, Slug = slug, Title = template.Title, Description = template.Description, DateCreated = DateTime.UtcNow }; _db.Templates.Add(newTemplate); await _db.SaveChangesAsync(); await _db.Entry(newTemplate).Reference(d => d.Creator).LoadAsync(); var newTemplateText = new TemplateText { TemplateId = newTemplate.TemplateId, EditorId = UserId, Content = template.Content, QuillDelta = template.QuillDelta, TimeUpdated = DateTime.UtcNow }; _db.TemplateText.Add(newTemplateText); await _db.SaveChangesAsync(); return(Ok(new TemplateDto(newTemplate, template.Content))); }
public async Task <InviteDto> VerifyInviteToken(string token) { var dbToken = await _db.InviteTokens.SingleOrDefaultAsync(t => t.Token == token); if (dbToken == null) { throw new Exception("Token not found"); } var user = await _db.Users.SingleOrDefaultAsync(u => u.Email == dbToken.Email); if (user != null) { _db.InviteTokens.Remove(dbToken); await _db.SaveChangesAsync(); throw new Exception("User with corresponding email is already registered"); } return(new InviteDto { Email = dbToken.Email, TimeCreated = dbToken.TimeCreated }); }
public async Task <IActionResult> Create([FromBody] ProjectCreateDto project) { if (project == null) { return(BadRequest()); } int attemptsCount = 0; int maxAttemptsCount = 100; string slug; bool isSlugUnique = false; do { slug = project.Name.GenerateSlug(); isSlugUnique = (await _db.Projects.SingleOrDefaultAsync(p => p.Slug == slug)) == null; } while (!isSlugUnique && attemptsCount < maxAttemptsCount); if (!isSlugUnique) { return(BadRequest(new { message = "Cannot generate unique slug" })); } // Creating new project var newProject = new Projects { Slug = slug, Name = project.Name, Description = project.Description, Goal = project.Goal, DateStart = project.DateStart, DateEnd = project.DateEnd, Avatar = project.Avatar, IsOpen = project.IsOpen, IsActive = project.IsActive }; _db.Projects.Add(newProject); await _db.SaveChangesAsync(); // Assigning team to project foreach (var member in project.Team) { if (member == null) { continue; } if (member.ProjectRole == null) { continue; } var role = await _db.ProjectRoles.SingleOrDefaultAsync(r => r.ProjectRoleId == member.ProjectRole.ProjectRoleId); if (role == null) { continue; } var existingMember = await _db.ProjectTeam.SingleOrDefaultAsync(t => t.UserId == member.UserId && t.ProjectId == newProject.ProjectId); if (existingMember != null) { continue; } var newMember = new ProjectTeam { UserId = member.UserId, ProjectId = newProject.ProjectId, ProjectRoleId = member.ProjectRole.ProjectRoleId, DateJoined = DateTime.UtcNow, Position = member.Position }; _db.ProjectTeam.Add(newMember); } await _db.SaveChangesAsync(); // Copying template for project var projectIntroTemplate = await _db.Templates.SingleOrDefaultAsync(t => t.TemplateTypeSlug == "project_intro" && t.ProjectId == null); if (projectIntroTemplate != null) { var newTemplate = new Templates { TemplateTypeSlug = projectIntroTemplate.TemplateTypeSlug, ProjectId = newProject.ProjectId, CreatorId = UserId, Slug = newProject.Name.GenerateSlug(), Title = projectIntroTemplate.Title, Description = projectIntroTemplate.Description, DateCreated = DateTime.UtcNow }; _db.Templates.Add(newTemplate); await _db.SaveChangesAsync(); var templateText = await _db.TemplateText.SingleOrDefaultAsync(t => t.TemplateId == projectIntroTemplate.TemplateId && t.IsActual); if (templateText != null) { var newTemplateText = new TemplateText { TemplateId = newTemplate.TemplateId, EditorId = UserId, Content = templateText.Content, QuillDelta = templateText.QuillDelta, TimeUpdated = DateTime.UtcNow }; _db.TemplateText.Add(newTemplateText); await _db.SaveChangesAsync(); } } await _db.Entry(newProject).Collection(b => b.QuickLinksHousingProject).LoadAsync(); var membersCount = await _db.ProjectTeam.Where(pt => pt.ProjectId == newProject.ProjectId).CountAsync(); var permissions = await _dbconnection.QueryAsync <string>( @"select pp.project_permission_slug from project_permissions as pp left join project_role_permissions as prp on prp.project_permission_slug = pp.project_permission_slug left join project_team as pt on pt.project_role_id = prp.project_role_id where pt.user_id = @user_id and pt.project_id = @project_id group by pp.project_permission_slug", new { user_id = UserId, project_id = newProject.ProjectId }); return(Ok(new ProjectDto(newProject, membersCount, permissions))); }
public async Task <IActionResult> Update([FromRoute] int id, [FromBody] UserDto updatedUser) { if (updatedUser == null) { return(BadRequest()); } var user = await GetById(id); if (user == null) { return(NotFound()); } // If email was changed if (updatedUser.Email != user.Email) { var userByEmail = await _db.Users.SingleOrDefaultAsync(u => u.Email == updatedUser.Email); if (userByEmail != null) { throw new Exception("Username with provided email already exists."); } user.Email = updatedUser.Email; } user.Name = updatedUser.Name; user.Surname = updatedUser.Surname; user.Avatar = updatedUser.Avatar; if (user.UserCompetences == null) { user.UserCompetences = new List <UserCompetences>(); } if (user.UserRoles == null) { user.UserRoles = new List <UserRoles>(); } if (updatedUser.Competences != null) { var toDelete = user.UserCompetences.Where(uc => !updatedUser.Competences.Select(c => c.CompetenceId).Contains(uc.CompetenceId) && uc.UserId == id).Select(uc => uc.CompetenceId); var toAdd = updatedUser.Competences.Where(u => !user.UserCompetences.Select(uc => uc.CompetenceId).Contains(u.CompetenceId)).Select(c => c.CompetenceId); _db.UserCompetences.RemoveRange(_db.UserCompetences.Where(uc => toDelete.Contains(uc.CompetenceId) && uc.UserId == id)); var existingCompetencesToAdd = await _db.Competences.Where(uc => toAdd.Contains(uc.CompetenceId)).ToListAsync(); _db.UserCompetences.AddRange(existingCompetencesToAdd.Select(c => new UserCompetences { UserId = id, CompetenceId = c.CompetenceId })); } if (updatedUser.Roles != null) { var toDelete = user.UserRoles.Where(uc => !updatedUser.Roles.Select(c => c.RoleId).Contains(uc.RoleId) && uc.UserId == id).Select(uc => uc.RoleId); var toAdd = updatedUser.Roles.Where(u => !user.UserRoles.Select(uc => uc.RoleId).Contains(u.RoleId)).Select(c => c.RoleId); _db.UserRoles.RemoveRange(_db.UserRoles.Where(uc => toDelete.Contains(uc.RoleId) && uc.UserId == id)); var existingRolesToAdd = await _db.Roles.Where(uc => toAdd.Contains(uc.RoleId)).ToListAsync(); _db.UserRoles.AddRange(existingRolesToAdd.Select(c => new UserRoles { UserId = id, RoleId = c.RoleId })); } await _db.SaveChangesAsync(); return(Ok(new UserDto(user))); }
public async Task <IActionResult> LikeDocument([FromRoute] int?id, [FromRoute] string slug) { var document = await GetDocument(id, slug); if (document == null) { return(NotFound()); } var existingLike = await _db.DocumentLikes.SingleOrDefaultAsync(l => l.UserId == UserId && l.DocumentId == document.DocumentId); if (existingLike == null) { var like = new DocumentLikes { UserId = UserId, DocumentId = document.DocumentId, TimeCreated = DateTime.UtcNow }; _db.DocumentLikes.Add(like); } else { _db.DocumentLikes.Remove(existingLike); } await _db.SaveChangesAsync(); return(Ok()); }