public async Task CreateComponentRating(RatingComponentModel ratingComponentModel) { await using (_context = new Sep3DBContext()) { bool exists = await _context.RatingComponent.AnyAsync(r => r.AccountModelUserId == ratingComponentModel.AccountModelUserId && r.ComponentModelId == ratingComponentModel.ComponentModelId); if (exists) { List <RatingComponentModel> list = await _context.RatingComponent .Where(r => r.ComponentModelId == ratingComponentModel.ComponentModelId && r.AccountModelUserId == ratingComponentModel.AccountModelUserId).ToListAsync(); RatingComponentModel rating = list[0]; rating.Score = ratingComponentModel.Score; _context.RatingComponent.Update(rating); await _context.SaveChangesAsync(); } else { Console.WriteLine("rating nu exista "); await _context.RatingComponent.AddAsync(ratingComponentModel); await _context.SaveChangesAsync(); } } }
public async Task EditBuild(BuildModel buildModel) { await using (_context = new Sep3DBContext()) { BuildModel buildModelDatabase = await _context.Builds .Include(b => b.BuildComponents) .FirstAsync(bld => bld.Id == buildModel.Id); buildModelDatabase.Name = buildModel.Name; buildModelDatabase.BuildComponents = new Collection <BuildComponent>(); for (var i = 0; i < buildModel.ComponentList.Count(); i++) { ComponentModel arg = await _context.Components .FirstAsync(c => c.Id == buildModel.ComponentList[i].Id); BuildComponent buildComponent = new BuildComponent { BuildId = buildModelDatabase.Id, BuildModel = buildModelDatabase, ComponentId = arg.Id, ComponentModel = arg }; buildModelDatabase.BuildComponents.Add(buildComponent); } _context.Update(buildModelDatabase); await _context.SaveChangesAsync(); } }
public async Task SavePost(PostModel postModel, int userId) { await using (_context = new Sep3DBContext()) { PostModel databasePostModel = await _context.Posts .FirstAsync(p => p.Id == postModel.Id); AccountModel accountModel = await _context.Accounts .Include(acc => acc.SavedPosts) .FirstAsync(a => a.UserId == userId); if (accountModel.SavedPosts == null) { accountModel.SavedPosts = new List <AccountSavedPost>(); } AccountSavedPost accountSavedPost = new AccountSavedPost() { AccountId = accountModel.UserId, AccountModel = accountModel, SavedPostId = databasePostModel.Id, SavedPostModel = databasePostModel }; accountModel.SavedPosts.Add(accountSavedPost); _context.Accounts.Update(accountModel); await _context.SaveChangesAsync(); } }
public async Task DeleteAccount(int userId) { await using (_context = new Sep3DBContext()) { AccountModel accountModel = await _context.Accounts .Include(a => a.Builds) .ThenInclude(b => b.BuildComponents) .Include(acc => acc.Posts) .ThenInclude(p => p.Comments) .FirstAsync(account => account.UserId == userId); foreach (var build in accountModel.Builds) { build.BuildComponents = new Collection <BuildComponent>(); } foreach (var post in accountModel.Posts) { post.Comments = new Collection <CommentModel>(); } foreach (var variable in _context.AccountFollowedAccounts) { if (variable.AccountModelUserId == userId) { _context.AccountFollowedAccounts.Remove(variable); } } accountModel.Builds = new Collection <BuildModel>(); accountModel.Posts = new Collection <PostModel>(); _context.Accounts.Remove(accountModel); await _context.SaveChangesAsync(); } }
public async Task <string> CreateAccount(AccountModel accountModel) { await using (_context = new Sep3DBContext()) { foreach (var variable in _context.Accounts) { if (variable.Username.Equals(accountModel.Username)) { Console.WriteLine("Account already exists"); return("Account already exists"); } } accountModel.Builds = new List <BuildModel>(); accountModel.Comments = new List <CommentModel>(); accountModel.Posts = new List <PostModel>(); accountModel.BuildRatings = new List <RatingBuildModel>(); accountModel.PostRatings = new List <RatingPostModel>(); accountModel.ComponentRatings = new List <RatingComponentModel>(); accountModel.SavedPosts = new List <AccountSavedPost>(); accountModel.FollowedAccounts = new List <AccountModel>(); await _context.Accounts.AddAsync(accountModel); Console.WriteLine("Account successfully created"); await _context.SaveChangesAsync(); } return("Account successfully created"); }
public async Task EditPost(PostModel postModel) { await using (_context = new Sep3DBContext()) { _context.Posts.Update(postModel); await _context.SaveChangesAsync(); } }
public async Task EditComponent(ComponentModel componentModel) { await using (_context = new Sep3DBContext()) { _context.Components.Update(componentModel); await _context.SaveChangesAsync(); } }
public async Task CreateBuild(BuildModel buildModel) { await using (_context = new Sep3DBContext()) { buildModel.Ratings = new List <RatingBuildModel>(); await _context.Builds.AddAsync(buildModel); await _context.SaveChangesAsync(); AccountModel accountModel = await _context.Accounts .Include(a => a.Builds) .FirstAsync(account => account.UserId == buildModel.AccountModelUserId); BuildModel databaseBuild = await _context.Builds.OrderBy(b => b.Id).LastAsync(); if (accountModel.Builds == null) { accountModel.Builds = new List <BuildModel>(); } accountModel.Builds.Add(databaseBuild); _context.Update(accountModel); if (databaseBuild.BuildComponents == null) { databaseBuild.BuildComponents = new List <BuildComponent>(); } for (var i = 0; i < databaseBuild.ComponentList.Count; i++) { ComponentModel arg = await _context.Components.FirstAsync(c => c.Id == databaseBuild.ComponentList[i].Id); BuildComponent buildComponent = new BuildComponent { BuildId = databaseBuild.Id, BuildModel = databaseBuild, ComponentId = arg.Id, ComponentModel = arg }; databaseBuild.BuildComponents.Add(buildComponent); } _context.Update(databaseBuild); await _context.SaveChangesAsync(); } }
public async Task CreateComponent(ComponentModel componentModel) { await using (_context = new Sep3DBContext()) { componentModel.Ratings = new Collection <RatingComponentModel>(); await _context.Components.AddAsync(componentModel); await _context.SaveChangesAsync(); } }
public async Task DeleteReport(int reportId) { await using (_context = new Sep3DBContext()) { ReportModel reportModel = await _context.Reports .FirstAsync(r => r.Id == reportId); _context.Reports.Remove(reportModel); await _context.SaveChangesAsync(); } }
public async Task DeleteComponent(int componentId) { await using (_context = new Sep3DBContext()) { ComponentModel component = await _context.Components .Include(c => c.Ratings) .Include(c => c.BuildComponents) .FirstAsync(c => c.Id == componentId); _context.Components.Remove(component); await _context.SaveChangesAsync(); } }
public async Task RemoveComponentFromBuild(int buildId, int componentId) { await using (_context = new Sep3DBContext()) { BuildComponent buildComponent = _context.Builds .Where(b => b.Id == buildId) .SelectMany(build => build.BuildComponents) .First(build => build.ComponentModel.Id == componentId); _context.Remove(buildComponent); Console.WriteLine("removed component"); await _context.SaveChangesAsync(); } }
public async Task DeleteBuild(int buildId) { await using (_context = new Sep3DBContext()) { BuildModel buildModel = await _context.Builds .Include(b => b.BuildComponents) .Include(r => r.Ratings) .FirstAsync(build => build.Id == buildId); buildModel.BuildComponents = new Collection <BuildComponent>(); _context.Builds.Remove(buildModel); await _context.SaveChangesAsync(); } }
public async Task CreateReport(ReportModel reportModel) { await using (_context = new Sep3DBContext()) { PostModel postModel = await _context.Posts .Include(post => post.Reports) .FirstAsync(p => p.Id == reportModel.PostModelId); postModel.Reports.Add(reportModel); _context.Posts.Update(postModel); await _context.Reports.AddAsync(reportModel); await _context.SaveChangesAsync(); } }
public async Task DeletePost(int postId) { await using (_context = new Sep3DBContext()) { PostModel postModel = await _context.Posts .Include(p => p.SavedPosts) .Include(po => po.Comments) .Include(pos => pos.Ratings) .Include(pp => pp.Reports) .FirstAsync(post => post.Id == postId); postModel.SavedPosts = new List <AccountSavedPost>(); postModel.Comments = new List <CommentModel>(); postModel.Ratings = new Collection <RatingPostModel>(); postModel.Reports = new List <ReportModel>(); _context.Posts.Remove(postModel); await _context.SaveChangesAsync(); } }
public async Task <string> EditAccount(AccountModel accountModel) { await using (_context = new Sep3DBContext()) { AccountModel accountModelDatabase = await _context.Accounts .Include(a => a.Posts) .FirstAsync(acc => acc.UserId == accountModel.UserId); accountModelDatabase.Username = accountModel.Username; accountModelDatabase.Password = accountModel.Password; accountModelDatabase.Name = accountModel.Name; foreach (var post in accountModelDatabase.Posts) { post.Username = accountModel.Username; } _context.Accounts.Update(accountModelDatabase); await _context.SaveChangesAsync(); } return("Account updated"); }
public async Task CreatePost(PostModel postModel) { await using (_context = new Sep3DBContext()) { AccountModel accountModel = await _context.Accounts .Include(acc => acc.Posts) .FirstAsync(a => a.UserId == postModel.AccountModelUserId); postModel.Username = accountModel.Username; if (accountModel.Posts == null) { accountModel.Posts = new List <PostModel>(); } postModel.Comments = new List <CommentModel>(); postModel.Ratings = new List <RatingPostModel>(); accountModel.Posts.Add(postModel); _context.Accounts.Update(accountModel); await _context.Posts.AddAsync(postModel); await _context.SaveChangesAsync(); } }
public async Task FollowAccount(int userId, int followId) { await using (_context = new Sep3DBContext()) { AccountModel accountModel = await _context.Accounts .FirstAsync(a => a.UserId == userId); AccountModel follow = await _context.Accounts .FirstAsync(a => a.UserId == followId); AccountFollowedAccount fol = new AccountFollowedAccount() { AccountModelUserId = userId, AccountModel = accountModel, FollowedAccountModelUserId = followId, FollowedAccountModel = follow }; await _context.AccountFollowedAccounts.AddAsync(fol); await _context.SaveChangesAsync(); } }
public async Task CreateComment(CommentModel commentModel) { await using (_context = new Sep3DBContext()) { AccountModel accountModel = await _context.Accounts .Include(acc => acc.Comments) .FirstAsync(a => a.UserId == commentModel.AccountModelUserId); commentModel.Username = accountModel.Username; await _context.Comments.AddAsync(commentModel); accountModel.Comments.Add(commentModel); _context.Accounts.Update(accountModel); PostModel postModel = await _context.Posts .Include(p => p.Comments) .FirstAsync(post => post.Id == commentModel.PostModelId); postModel.Comments.Add(commentModel); _context.Posts.Update(postModel); await _context.SaveChangesAsync(); } }
public async Task AddComponentToBuild(int buildId, int componentId) { await using (_context = new Sep3DBContext()) { BuildModel buildModel = await _context.Builds.FirstAsync(b => b.Id == buildId); ComponentModel componentModel = await _context.Components .Include(c => c.Ratings) .FirstAsync(c => c.Id == componentId); BuildComponent buildComponent = new BuildComponent { BuildId = buildModel.Id, BuildModel = buildModel, ComponentId = componentModel.Id, ComponentModel = componentModel }; if (buildModel.BuildComponents == null) { buildModel.BuildComponents = new List <BuildComponent>(); buildModel.BuildComponents.Add(buildComponent); Console.WriteLine("added from null"); } else { buildModel.BuildComponents.Add(buildComponent); Console.WriteLine("added from existed"); } foreach (var variable in buildModel.BuildComponents) { Console.WriteLine("buildId= " + variable.BuildId + "\n Build model= " + variable.BuildModel + "componentId= " + variable.ComponentId + "\n Component model= " + variable.ComponentModel); } _context.Update(buildModel); await _context.SaveChangesAsync(); } }
public async Task UnfollowAccount(int userId, int followId) { await using (_context = new Sep3DBContext()) { AccountModel accountModel = await _context.Accounts .FirstAsync(a => a.UserId == userId); AccountModel follow = await _context.Accounts .FirstAsync(a => a.UserId == followId); foreach (var variable in _context.AccountFollowedAccounts) { if (variable.AccountModelUserId == userId && variable.FollowedAccountModelUserId == followId) { _context.AccountFollowedAccounts.Remove(variable); await _context.SaveChangesAsync(); return; } } } }