BlazorStart/Start/Server/Data/Services/BookmarkService.cs

80 lines
2.1 KiB
C#
Raw Normal View History

2021-11-16 06:27:28 +00:00
using System.Collections.Generic;
2021-11-13 03:21:59 +00:00
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Start.Server.Data.Services.Interfaces;
using Start.Server.Models;
namespace Start.Server.Data.Services {
public class BookmarkService : IBookmarkService {
private readonly ApplicationDbContext db;
public BookmarkService(ApplicationDbContext dbContext) {
this.db = dbContext;
}
2021-11-16 06:27:28 +00:00
public Bookmark? GetBookmark(string userId, int bookmarkId) {
2021-11-14 01:19:28 +00:00
if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId))
2021-11-16 06:27:28 +00:00
return null;
2021-11-13 03:21:59 +00:00
2021-11-16 06:27:28 +00:00
return this.db.Bookmarks
2021-11-13 03:21:59 +00:00
.SingleOrDefault(b => b.BookmarkId == bookmarkId);
}
public IList<Bookmark> GetUserBookmarks(string userId) {
return this.db.Bookmarks
.Where(b => b.BookmarkGroup!.BookmarkContainer!.ApplicationUserId == userId)
.ToList();
}
2021-11-16 06:27:28 +00:00
public Bookmark? CreateBookmark(string userId, string title, string url, string? notes,
2021-11-13 03:21:59 +00:00
int bookmarkGroupId) {
2021-11-14 01:19:28 +00:00
if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(this.db, userId, bookmarkGroupId))
2021-11-16 06:27:28 +00:00
return null;
2021-11-13 03:21:59 +00:00
Bookmark newBookmark = new(title, url, bookmarkGroupId);
db.Bookmarks.Add(newBookmark);
db.SaveChanges();
2021-11-16 06:27:28 +00:00
if (newBookmark.BookmarkId <= 0)
return null;
return newBookmark;
2021-11-13 03:21:59 +00:00
}
2021-11-16 06:27:28 +00:00
public Bookmark? UpdateBookmark(string userId, Bookmark bookmark) {
2021-11-13 03:21:59 +00:00
Bookmark? existingBookmark = db.Bookmarks
.SingleOrDefault(b => b.BookmarkId == bookmark.BookmarkId);
2021-11-14 01:19:28 +00:00
if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmark.BookmarkId))
2021-11-16 06:27:28 +00:00
return null;
2021-11-13 03:21:59 +00:00
2021-11-16 06:27:28 +00:00
// Could be moving to a different group
2021-11-14 01:19:28 +00:00
if (!BookmarkOwnershipTools
.IsBookmarkGroupOwner(this.db, userId, bookmark.BookmarkGroupId))
2021-11-16 06:27:28 +00:00
return null;
2021-11-13 03:21:59 +00:00
db.Entry(bookmark).State = EntityState.Modified;
db.SaveChanges();
2021-11-16 06:27:28 +00:00
return bookmark;
2021-11-13 03:21:59 +00:00
}
2021-11-16 06:27:28 +00:00
public bool DeleteBookmark(string userId, int bookmarkId) {
2021-11-13 03:21:59 +00:00
Bookmark? bookmark = db.Bookmarks
.SingleOrDefault(b => b.BookmarkId == bookmarkId);
if (bookmark == null)
2021-11-16 06:27:28 +00:00
return false;
2021-11-13 03:21:59 +00:00
2021-11-14 01:19:28 +00:00
if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId))
2021-11-16 06:27:28 +00:00
return false;
2021-11-13 03:21:59 +00:00
db.Bookmarks.Remove(bookmark);
db.SaveChanges();
2021-11-16 06:27:28 +00:00
return true;
2021-11-13 03:21:59 +00:00
}
}
}