2021-11-14 01:19:28 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Start.Server.Data.Services.Interfaces;
|
2021-11-14 03:27:01 +00:00
|
|
|
|
using Start.Server.Extensions;
|
2021-11-14 01:19:28 +00:00
|
|
|
|
using Start.Server.Models;
|
2021-11-16 05:57:29 +00:00
|
|
|
|
using Start.Shared;
|
2021-11-14 01:19:28 +00:00
|
|
|
|
|
|
|
|
|
namespace Start.Server.Data.Services {
|
|
|
|
|
public class BookmarkGroupService : IBookmarkGroupService {
|
|
|
|
|
private readonly ApplicationDbContext db;
|
|
|
|
|
|
|
|
|
|
public BookmarkGroupService(ApplicationDbContext dbContext) {
|
|
|
|
|
this.db = dbContext;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-16 06:27:28 +00:00
|
|
|
|
public BookmarkGroup? GetBookmarkGroup(string userId, int bookmarkGroupId,
|
|
|
|
|
bool includeBookmarks = false) {
|
2021-11-14 01:19:28 +00:00
|
|
|
|
BookmarkGroup? group = db.BookmarkGroups
|
|
|
|
|
.Where(bg => bg.BookmarkGroupId == bookmarkGroupId)
|
|
|
|
|
.If(includeBookmarks, q => q.Include(bg => bg.Bookmarks))
|
|
|
|
|
.SingleOrDefault();
|
|
|
|
|
|
|
|
|
|
if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(db, userId, bookmarkGroupId))
|
2021-11-16 06:27:28 +00:00
|
|
|
|
return null;
|
2021-11-14 01:19:28 +00:00
|
|
|
|
|
2021-11-16 06:27:28 +00:00
|
|
|
|
return group;
|
2021-11-14 01:19:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IList<BookmarkGroup> GetUserBookmarkGroups(string userId,
|
|
|
|
|
bool includeBookmarkGroups = false) {
|
|
|
|
|
return this.db.BookmarkGroups
|
|
|
|
|
.Where(bg => bg.BookmarkContainer!.ApplicationUserId == userId)
|
|
|
|
|
.If(includeBookmarkGroups, q => q.Include(bg => bg.Bookmarks))
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-16 06:27:28 +00:00
|
|
|
|
public BookmarkGroup? CreateBookmarkGroup(string userId, string title,
|
2021-11-14 01:19:28 +00:00
|
|
|
|
string color, int bookmarkContainerId) {
|
|
|
|
|
if (!BookmarkOwnershipTools
|
|
|
|
|
.IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId))
|
2021-11-16 06:27:28 +00:00
|
|
|
|
return null;
|
2021-11-14 01:19:28 +00:00
|
|
|
|
|
|
|
|
|
BookmarkGroup newBookmarkGroup = new(title, color, bookmarkContainerId);
|
|
|
|
|
this.db.BookmarkGroups.Add(newBookmarkGroup);
|
|
|
|
|
this.db.SaveChanges();
|
|
|
|
|
|
2021-11-16 06:27:28 +00:00
|
|
|
|
return newBookmarkGroup;
|
2021-11-14 01:19:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-16 06:27:28 +00:00
|
|
|
|
public BookmarkGroup? UpdateBookmarkGroup(string userId,
|
2021-11-14 01:19:28 +00:00
|
|
|
|
BookmarkGroup bookmarkGroup) {
|
|
|
|
|
BookmarkGroup? existingGroup = this.db.BookmarkGroups
|
|
|
|
|
.SingleOrDefault(bg => bg.BookmarkGroupId == bookmarkGroup.BookmarkGroupId);
|
|
|
|
|
|
|
|
|
|
if (existingGroup == null)
|
2021-11-16 06:27:28 +00:00
|
|
|
|
return null;
|
2021-11-14 01:19:28 +00:00
|
|
|
|
|
|
|
|
|
if (!BookmarkOwnershipTools
|
|
|
|
|
.IsBookmarkGroupOwner(this.db, userId, bookmarkGroup.BookmarkGroupId))
|
2021-11-16 06:27:28 +00:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
if (!BookmarkOwnershipTools
|
|
|
|
|
.IsBookmarkContainerOwner(this.db, userId, bookmarkGroup.BookmarkContainerId))
|
|
|
|
|
return null;
|
2021-11-14 01:19:28 +00:00
|
|
|
|
|
|
|
|
|
this.db.Entry(bookmarkGroup).State = EntityState.Modified;
|
|
|
|
|
this.db.SaveChanges();
|
|
|
|
|
|
2021-11-16 06:27:28 +00:00
|
|
|
|
return bookmarkGroup;
|
2021-11-14 01:19:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-16 06:27:28 +00:00
|
|
|
|
public bool DeleteBookmarkGroup(string userId, int bookmarkGroupId) {
|
2021-11-14 01:19:28 +00:00
|
|
|
|
BookmarkGroup? bookmarkGroup = this.db.BookmarkGroups
|
|
|
|
|
.SingleOrDefault(bg => bg.BookmarkGroupId == bookmarkGroupId);
|
|
|
|
|
|
|
|
|
|
if (bookmarkGroup == null)
|
2021-11-16 06:27:28 +00:00
|
|
|
|
return false;
|
2021-11-14 01:19:28 +00:00
|
|
|
|
|
|
|
|
|
if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(this.db, userId, bookmarkGroupId))
|
2021-11-16 06:27:28 +00:00
|
|
|
|
return false;
|
2021-11-14 01:19:28 +00:00
|
|
|
|
|
|
|
|
|
this.db.BookmarkGroups.Remove(bookmarkGroup);
|
|
|
|
|
this.db.SaveChanges();
|
|
|
|
|
|
2021-11-16 06:27:28 +00:00
|
|
|
|
return true;
|
2021-11-14 01:19:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|