2021-11-13 03:21:59 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Security;
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public (BookmarkStatus, Bookmark?) GetBookmark(string userId, int bookmarkId) {
|
2021-11-14 01:19:28 +00:00
|
|
|
|
if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId))
|
2021-11-13 03:21:59 +00:00
|
|
|
|
return (BookmarkStatus.OwnerDoesNotMatch, null);
|
|
|
|
|
|
|
|
|
|
Bookmark? bookmark = this.db.Bookmarks
|
|
|
|
|
.SingleOrDefault(b => b.BookmarkId == bookmarkId);
|
|
|
|
|
|
|
|
|
|
if (bookmark == null)
|
|
|
|
|
return (BookmarkStatus.BookmarkDoesNotExist, null);
|
|
|
|
|
|
|
|
|
|
return (BookmarkStatus.OK, bookmark);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IList<Bookmark> GetUserBookmarks(string userId) {
|
|
|
|
|
return this.db.Bookmarks
|
|
|
|
|
.Where(b => b.BookmarkGroup!.BookmarkContainer!.ApplicationUserId == userId)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-14 01:19:28 +00:00
|
|
|
|
public (BookmarkStatus, 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))
|
|
|
|
|
return (BookmarkStatus.OwnerDoesNotMatch, null);
|
2021-11-13 03:21:59 +00:00
|
|
|
|
|
|
|
|
|
Bookmark newBookmark = new(title, url, bookmarkGroupId);
|
|
|
|
|
|
|
|
|
|
db.Bookmarks.Add(newBookmark);
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
|
2021-11-14 01:19:28 +00:00
|
|
|
|
return (BookmarkStatus.OK, newBookmark);
|
2021-11-13 03:21:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public (BookmarkStatus, Bookmark?) UpdateBookmark(string userId, Bookmark bookmark) {
|
|
|
|
|
Bookmark? existingBookmark = db.Bookmarks
|
|
|
|
|
.SingleOrDefault(b => b.BookmarkId == bookmark.BookmarkId);
|
|
|
|
|
|
|
|
|
|
if (existingBookmark == null)
|
|
|
|
|
return (BookmarkStatus.BookmarkDoesNotExist, null);
|
|
|
|
|
|
2021-11-14 01:19:28 +00:00
|
|
|
|
if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmark.BookmarkId))
|
2021-11-13 03:21:59 +00:00
|
|
|
|
return (BookmarkStatus.OwnerDoesNotMatch, null);
|
|
|
|
|
|
2021-11-14 01:19:28 +00:00
|
|
|
|
if (!BookmarkOwnershipTools
|
|
|
|
|
.IsBookmarkGroupOwner(this.db, userId, bookmark.BookmarkGroupId))
|
2021-11-13 03:21:59 +00:00
|
|
|
|
return (BookmarkStatus.OwnerDoesNotMatch, null);
|
|
|
|
|
|
|
|
|
|
db.Entry(bookmark).State = EntityState.Modified;
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return (BookmarkStatus.OK, bookmark);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public BookmarkStatus DeleteBookmark(string userId, int bookmarkId) {
|
|
|
|
|
Bookmark? bookmark = db.Bookmarks
|
|
|
|
|
.SingleOrDefault(b => b.BookmarkId == bookmarkId);
|
|
|
|
|
|
|
|
|
|
if (bookmark == null)
|
|
|
|
|
return BookmarkStatus.BookmarkDoesNotExist;
|
|
|
|
|
|
2021-11-14 01:19:28 +00:00
|
|
|
|
if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId))
|
2021-11-13 03:21:59 +00:00
|
|
|
|
return BookmarkStatus.OwnerDoesNotMatch;
|
|
|
|
|
|
|
|
|
|
db.Bookmarks.Remove(bookmark);
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return BookmarkStatus.OK;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|