From b00158daa7108ad04a893d43adee5d5ff6979e03 Mon Sep 17 00:00:00 2001 From: Neil Brommer Date: Sun, 28 Nov 2021 22:32:21 -0800 Subject: [PATCH] Use Refit for APIs, make data/API stack async --- Start/Client/Components/CreateContainer.razor | 18 ++++----- Start/Client/Components/DeleteContainer.razor | 7 +++- Start/Client/Pages/StartPage.razor | 36 +++++++++-------- Start/Client/Program.cs | 22 +++++++++-- Start/Client/Start.Client.csproj | 4 ++ .../BookmarkContainersController.cs | 23 +++++------ .../Server/Controllers/BookmarksController.cs | 20 +++++----- .../Data/Services/BookmarkContainerService.cs | 35 +++++++++-------- .../Data/Services/BookmarkGroupService.cs | 39 +++++++++---------- Start/Server/Data/Services/BookmarkService.cs | 27 ++++++------- .../Interfaces/IBookmarkContainerService.cs | 15 ++++--- .../Interfaces/IBookmarkGroupService.cs | 11 +++--- .../Services/Interfaces/IBookmarkService.cs | 15 ++++--- Start/Server/Startup.cs | 2 + Start/Shared/Api/IBookmarkContainersApi.cs | 21 ++++++++++ Start/Shared/Api/IBookmarksApi.cs | 15 +++++++ Start/Shared/Start.Shared.csproj | 10 +++++ 17 files changed, 196 insertions(+), 124 deletions(-) create mode 100644 Start/Shared/Api/IBookmarkContainersApi.cs create mode 100644 Start/Shared/Api/IBookmarksApi.cs diff --git a/Start/Client/Components/CreateContainer.razor b/Start/Client/Components/CreateContainer.razor index 1d8bc50..4a7da0e 100644 --- a/Start/Client/Components/CreateContainer.razor +++ b/Start/Client/Components/CreateContainer.razor @@ -1,6 +1,8 @@ @using Start.Shared -@using System.IO -@inject HttpClient Http +@using Start.Shared.Api +@using Refit + +@inject IBookmarkContainersApi bookmarkContainersApi @@ -50,16 +52,10 @@ protected async void OnSubmit() { - HttpResponseMessage response = await Http - .PostAsJsonAsync("BookmarkContainers/Create", model.Title); + ApiResponse apiResponse = await bookmarkContainersApi + .CreateBookmarkContainer(model.Title); - Stream stream = response.RequestMessage!.Content!.ReadAsStream(); - StreamReader reader = new StreamReader(stream); - Console.WriteLine(reader.ReadToEnd()); - - BookmarkContainerDto? container = await response - !.Content - !.ReadFromJsonAsync(); + BookmarkContainerDto? container = apiResponse.Content; if (container == null) { diff --git a/Start/Client/Components/DeleteContainer.razor b/Start/Client/Components/DeleteContainer.razor index 9807f2d..6e94d27 100644 --- a/Start/Client/Components/DeleteContainer.razor +++ b/Start/Client/Components/DeleteContainer.razor @@ -1,5 +1,8 @@ @using Microsoft.AspNetCore.Components.WebAssembly.Authentication +@using Start.Shared.Api + @inject HttpClient Http +@inject IBookmarkContainersApi bookmarkContainersApi @{ string title = $"Delete Container \"{this.ContainerTitle}\""; } @@ -41,8 +44,8 @@ { try { - HttpResponseMessage result = await Http - .DeleteAsync($"BookmarkContainers/Delete/{this.BookmarkContainerId}"); + HttpResponseMessage result = await bookmarkContainersApi + .DeleteBookmarkContainer(this.BookmarkContainerId); if (result.StatusCode == System.Net.HttpStatusCode.OK) { diff --git a/Start/Client/Pages/StartPage.razor b/Start/Client/Pages/StartPage.razor index 7a2196b..5b4a167 100644 --- a/Start/Client/Pages/StartPage.razor +++ b/Start/Client/Pages/StartPage.razor @@ -1,11 +1,16 @@ @page "/Start" -@using Microsoft.AspNetCore.Authorization +@using System.Collections.Generic +@using System.Linq @using Microsoft.AspNetCore.Components.WebAssembly.Authentication @using Start.Client.Components @using Start.Shared -@attribute [Authorize] -@inject HttpClient Http +@using Start.Shared.Api +@using Refit + +@* Distiguish from Refit.Authorize *@ +@attribute [Microsoft.AspNetCore.Authorization.Authorize] @inject Blazored.LocalStorage.ILocalStorageService localStorage +@inject IBookmarkContainersApi bookmarkContainersApi @if (bookmarkContainers == null) { @@ -101,9 +106,10 @@ else { try { - this.bookmarkContainers = await Http - .GetFromJsonAsync>( - "BookmarkContainers"); + ApiResponse> response = await bookmarkContainersApi + .GetAllBookmarkContainers(); + + this.bookmarkContainers = response.Content?.ToList(); if (this.bookmarkContainers == null || !this.bookmarkContainers.Any()) { @@ -120,13 +126,10 @@ else protected async Task CreateDefaultContainer() { - HttpResponseMessage response = await Http - .PostAsJsonAsync("BookmarkContainers/Create", "Default"); + ApiResponse response = await bookmarkContainersApi + .CreateBookmarkContainer("Default"); - BookmarkContainerDto? container = await response - .RequestMessage - !.Content - !.ReadFromJsonAsync(); + BookmarkContainerDto? container = response.Content; if (container != null) await this.OnContainerSelected(container.BookmarkContainerId); @@ -139,12 +142,13 @@ else if (!this.bookmarkContainers?.Any(bc => bc.BookmarkContainerId == bookmarkContainerId) ?? false) bookmarkContainerId = this.bookmarkContainers?.First().BookmarkContainerId ?? bookmarkContainerId; - BookmarkContainerDto? bookmarkContainer = await Http - .GetFromJsonAsync( - $"BookmarkContainers/{bookmarkContainerId}"); + ApiResponse response = await bookmarkContainersApi + .GetBookmarkContainer(bookmarkContainerId); + + BookmarkContainerDto? container = response.Content; await this.SetSelectedContainer(bookmarkContainerId); - this.selectedBookmarkContainer = bookmarkContainer; + this.selectedBookmarkContainer = container; } catch (AccessTokenNotAvailableException e) { diff --git a/Start/Client/Program.cs b/Start/Client/Program.cs index fa0ff40..5769dff 100644 --- a/Start/Client/Program.cs +++ b/Start/Client/Program.cs @@ -5,6 +5,8 @@ using Microsoft.AspNetCore.Components.WebAssembly.Authentication; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using Microsoft.Extensions.DependencyInjection; using Blazored.LocalStorage; +using Refit; +using Start.Shared.Api; namespace Start.Client { public class Program { @@ -16,12 +18,26 @@ namespace Start.Client { client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)) .AddHttpMessageHandler(); - // Supply HttpClient instances that include access tokens when making requests to the server project + // Supply HttpClient instances that include access tokens when making requests to the + // server project builder.Services.AddScoped(sp => sp.GetRequiredService() - .CreateClient("Start.ServerAPI")); + .CreateClient("Start.ServerAPI")); + + // Blazor will throw an error if a relative URI is used, so we have to get the base + // address for building the API paths + Uri baseUri = new(builder.HostEnvironment.BaseAddress); + + builder.Services.AddRefitClient() + .ConfigureHttpClient(c => { + c.BaseAddress = new Uri(baseUri, "BookmarkContainers"); + }) + .AddHttpMessageHandler(); + + builder.Services.AddRefitClient() + .ConfigureHttpClient(c => { c.BaseAddress = new Uri(baseUri, "Bookmarks"); }) + .AddHttpMessageHandler(); builder.Services.AddApiAuthorization(); - builder.Services.AddBlazoredLocalStorage(); await builder.Build().RunAsync(); diff --git a/Start/Client/Start.Client.csproj b/Start/Client/Start.Client.csproj index 3d0ba30..d269baa 100644 --- a/Start/Client/Start.Client.csproj +++ b/Start/Client/Start.Client.csproj @@ -17,6 +17,8 @@ + + @@ -29,6 +31,8 @@ + + diff --git a/Start/Server/Controllers/BookmarkContainersController.cs b/Start/Server/Controllers/BookmarkContainersController.cs index ae108a6..951c017 100644 --- a/Start/Server/Controllers/BookmarkContainersController.cs +++ b/Start/Server/Controllers/BookmarkContainersController.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -22,9 +23,9 @@ namespace Start.Server.Controllers { [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IEnumerable))] [ProducesResponseType(StatusCodes.Status404NotFound)] - public IActionResult GetAllBookmarkContainers() { - List? containers = this.bookmarkContainerService - .GetUserBookmarkContainers(this.GetAuthorizedUserId()) + public async Task GetAllBookmarkContainers() { + List? containers = (await this.bookmarkContainerService + .GetUserBookmarkContainers(this.GetAuthorizedUserId())) .Select(bc => bc.MapToDto()) .ToList(); @@ -38,9 +39,9 @@ namespace Start.Server.Controllers { [Route("{bookmarkContainerId}")] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BookmarkContainerDto))] [ProducesResponseType(StatusCodes.Status404NotFound)] - public IActionResult GetBookmarkContainer(int bookmarkContainerId) { - BookmarkContainerDto? container = this.bookmarkContainerService - .GetBookmarkContainer(this.GetAuthorizedUserId(), bookmarkContainerId, true, true) + public async Task GetBookmarkContainer(int bookmarkContainerId) { + BookmarkContainerDto? container = (await this.bookmarkContainerService + .GetBookmarkContainer(this.GetAuthorizedUserId(), bookmarkContainerId, true, true)) ?.MapToDto(); if (container == null) @@ -53,9 +54,9 @@ namespace Start.Server.Controllers { [Route("Create")] [ProducesResponseType(StatusCodes.Status201Created, Type = typeof(BookmarkContainerDto))] [ProducesResponseType(StatusCodes.Status400BadRequest)] - public IActionResult CreateBookmarkContainer([FromBody] string title) { - BookmarkContainerDto? container = this.bookmarkContainerService - .CreateBookmarkContainer(this.GetAuthorizedUserId(), title) + public async Task CreateBookmarkContainer([FromBody] string title) { + BookmarkContainerDto? container = (await this.bookmarkContainerService + .CreateBookmarkContainer(this.GetAuthorizedUserId(), title)) ?.MapToDto(); if (container == null) @@ -71,8 +72,8 @@ namespace Start.Server.Controllers { [Route("Delete/{bookmarkContainerId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] - public IActionResult DeleteBookmarkContainer(int bookmarkContainerId) { - bool res = this.bookmarkContainerService + public async Task DeleteBookmarkContainer(int bookmarkContainerId) { + bool res = await this.bookmarkContainerService .DeleteBookmarkContainer(this.GetAuthorizedUserId(), bookmarkContainerId); if (!res) diff --git a/Start/Server/Controllers/BookmarksController.cs b/Start/Server/Controllers/BookmarksController.cs index 6252996..a0ab300 100644 --- a/Start/Server/Controllers/BookmarksController.cs +++ b/Start/Server/Controllers/BookmarksController.cs @@ -1,5 +1,4 @@ -using System.Collections.Generic; -using System.Linq; +using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -25,9 +24,9 @@ namespace Start.Server.Controllers { [Route("{bookmarkId}")] [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BookmarkDto))] [ProducesResponseType(StatusCodes.Status404NotFound)] - public IActionResult GetBookmark(int bookmarkId) { - BookmarkDto? bookmark = this.bookmarkService - .GetBookmark(this.GetAuthorizedUserId(), bookmarkId) + public async Task GetBookmark(int bookmarkId) { + BookmarkDto? bookmark = (await this.bookmarkService + .GetBookmark(this.GetAuthorizedUserId(), bookmarkId)) ?.MapToDto(); if (bookmark == null) @@ -40,10 +39,10 @@ namespace Start.Server.Controllers { [Route("Create")] [ProducesResponseType(StatusCodes.Status201Created, Type = typeof(BookmarkDto))] [ProducesResponseType(StatusCodes.Status400BadRequest)] - public IActionResult CreateBookmark(string title, string url, string? notes, + public async Task CreateBookmark(string title, string url, string? notes, int bookmarkGroupId) { - BookmarkDto? bookmark = this.bookmarkService - .CreateBookmark(this.GetAuthorizedUserId(), title, url, notes, bookmarkGroupId) + BookmarkDto? bookmark = (await this.bookmarkService + .CreateBookmark(this.GetAuthorizedUserId(), title, url, notes, bookmarkGroupId)) ?.MapToDto(); if (bookmark == null) @@ -58,8 +57,9 @@ namespace Start.Server.Controllers { [Route("Delete/{bookmarkId}")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] - public IActionResult DeleteBookmark(int bookmarkId) { - var res = this.bookmarkService.DeleteBookmark(this.GetAuthorizedUserId(), bookmarkId); + public async Task DeleteBookmark(int bookmarkId) { + var res = await this.bookmarkService + .DeleteBookmark(this.GetAuthorizedUserId(), bookmarkId); if (!res) return NotFound(); diff --git a/Start/Server/Data/Services/BookmarkContainerService.cs b/Start/Server/Data/Services/BookmarkContainerService.cs index 9f39df5..69f4aa2 100644 --- a/Start/Server/Data/Services/BookmarkContainerService.cs +++ b/Start/Server/Data/Services/BookmarkContainerService.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Start.Server.Data.Services.Interfaces; using Start.Server.Extensions; @@ -13,15 +14,15 @@ namespace Start.Server.Data.Services { this.db = dbContext; } - public BookmarkContainer? GetBookmarkContainer(string userId, + public async Task GetBookmarkContainer(string userId, int bookmarkContainerId, bool includeGroups = false, bool includeBookmarks = false) { - BookmarkContainer? bookmarkContainer = this.db.BookmarkContainers + BookmarkContainer? bookmarkContainer = await this.db.BookmarkContainers .Where(bc => bc.BookmarkContainerId == bookmarkContainerId) .If(includeGroups, q => q.Include(bc => bc.BookmarkGroups)) .If(includeBookmarks, q => q .Include(bc => bc.BookmarkGroups) .ThenInclude(bg => bg.Bookmarks)) - .SingleOrDefault(); + .SingleOrDefaultAsync(); if (bookmarkContainer == null) return null; @@ -33,31 +34,31 @@ namespace Start.Server.Data.Services { return bookmarkContainer; } - public IList GetUserBookmarkContainers(string userId, + public async Task> GetUserBookmarkContainers(string userId, bool includeGroups = false, bool includeBookmarks = false) { - return this.db.BookmarkContainers + return await this.db.BookmarkContainers .Where(bc => bc.ApplicationUserId == userId) .If(includeGroups, q => q.Include(bc => bc.BookmarkGroups)) .If(includeBookmarks, q => q .Include(bc => bc.BookmarkGroups) .ThenInclude(bg => bg.Bookmarks)) - .ToList(); + .ToListAsync(); } - public BookmarkContainer? CreateBookmarkContainer(string userId, + public async Task CreateBookmarkContainer(string userId, string title) { // No need to worry about ownership here BookmarkContainer newContainer = new(userId, title); - this.db.BookmarkContainers.Add(newContainer); - this.db.SaveChanges(); + await this.db.BookmarkContainers.AddAsync(newContainer); + await this.db.SaveChangesAsync(); return newContainer; } - public BookmarkContainer? UpdateBookmarkContainer(string userId, + public async Task UpdateBookmarkContainer(string userId, BookmarkContainer bookmarkContainer) { - BookmarkContainer? exitingBookmarkContainer = this.db.BookmarkContainers - .SingleOrDefault(bc => bc.BookmarkContainerId + BookmarkContainer? exitingBookmarkContainer = await this.db.BookmarkContainers + .SingleOrDefaultAsync(bc => bc.BookmarkContainerId == bookmarkContainer.BookmarkContainerId); if (exitingBookmarkContainer == null @@ -66,15 +67,15 @@ namespace Start.Server.Data.Services { return null; this.db.Entry(bookmarkContainer).State = EntityState.Modified; - this.db.SaveChanges(); + await this.db.SaveChangesAsync(); return bookmarkContainer; } - public bool DeleteBookmarkContainer(string userId, int bookmarkContainerId) { - BookmarkContainer? bookmarkContainer = this.db.BookmarkContainers + public async Task DeleteBookmarkContainer(string userId, int bookmarkContainerId) { + BookmarkContainer? bookmarkContainer = await this.db.BookmarkContainers .Where(bc => bc.BookmarkContainerId == bookmarkContainerId) - .SingleOrDefault(); + .SingleOrDefaultAsync(); if (bookmarkContainer == null) return false; @@ -83,7 +84,7 @@ namespace Start.Server.Data.Services { return false; this.db.BookmarkContainers.Remove(bookmarkContainer); - this.db.SaveChanges(); + await this.db.SaveChangesAsync(); return true; } diff --git a/Start/Server/Data/Services/BookmarkGroupService.cs b/Start/Server/Data/Services/BookmarkGroupService.cs index 1c651da..a700875 100644 --- a/Start/Server/Data/Services/BookmarkGroupService.cs +++ b/Start/Server/Data/Services/BookmarkGroupService.cs @@ -1,11 +1,10 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Start.Server.Data.Services.Interfaces; using Start.Server.Extensions; using Start.Server.Models; -using Start.Shared; namespace Start.Server.Data.Services { public class BookmarkGroupService : IBookmarkGroupService { @@ -15,12 +14,12 @@ namespace Start.Server.Data.Services { this.db = dbContext; } - public BookmarkGroup? GetBookmarkGroup(string userId, int bookmarkGroupId, + public async Task GetBookmarkGroup(string userId, int bookmarkGroupId, bool includeBookmarks = false) { - BookmarkGroup? group = db.BookmarkGroups + BookmarkGroup? group = await db.BookmarkGroups .Where(bg => bg.BookmarkGroupId == bookmarkGroupId) .If(includeBookmarks, q => q.Include(bg => bg.Bookmarks)) - .SingleOrDefault(); + .SingleOrDefaultAsync(); if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(db, userId, bookmarkGroupId)) return null; @@ -28,31 +27,31 @@ namespace Start.Server.Data.Services { return group; } - public IList GetUserBookmarkGroups(string userId, + public async Task> GetUserBookmarkGroups(string userId, bool includeBookmarkGroups = false) { - return this.db.BookmarkGroups + return await this.db.BookmarkGroups .Where(bg => bg.BookmarkContainer!.ApplicationUserId == userId) .If(includeBookmarkGroups, q => q.Include(bg => bg.Bookmarks)) - .ToList(); + .ToListAsync(); } - public BookmarkGroup? CreateBookmarkGroup(string userId, string title, + public async Task CreateBookmarkGroup(string userId, string title, string color, int bookmarkContainerId) { if (!BookmarkOwnershipTools .IsBookmarkContainerOwner(this.db, userId, bookmarkContainerId)) return null; BookmarkGroup newBookmarkGroup = new(title, color, bookmarkContainerId); - this.db.BookmarkGroups.Add(newBookmarkGroup); - this.db.SaveChanges(); + await this.db.BookmarkGroups.AddAsync(newBookmarkGroup); + await this.db.SaveChangesAsync(); return newBookmarkGroup; } - public BookmarkGroup? UpdateBookmarkGroup(string userId, + public async Task UpdateBookmarkGroup(string userId, BookmarkGroup bookmarkGroup) { - BookmarkGroup? existingGroup = this.db.BookmarkGroups - .SingleOrDefault(bg => bg.BookmarkGroupId == bookmarkGroup.BookmarkGroupId); + BookmarkGroup? existingGroup = await this.db.BookmarkGroups + .SingleOrDefaultAsync(bg => bg.BookmarkGroupId == bookmarkGroup.BookmarkGroupId); if (existingGroup == null) return null; @@ -66,14 +65,14 @@ namespace Start.Server.Data.Services { return null; this.db.Entry(bookmarkGroup).State = EntityState.Modified; - this.db.SaveChanges(); + await this.db.SaveChangesAsync(); return bookmarkGroup; } - public bool DeleteBookmarkGroup(string userId, int bookmarkGroupId) { - BookmarkGroup? bookmarkGroup = this.db.BookmarkGroups - .SingleOrDefault(bg => bg.BookmarkGroupId == bookmarkGroupId); + public async Task DeleteBookmarkGroup(string userId, int bookmarkGroupId) { + BookmarkGroup? bookmarkGroup = await this.db.BookmarkGroups + .SingleOrDefaultAsync(bg => bg.BookmarkGroupId == bookmarkGroupId); if (bookmarkGroup == null) return false; @@ -82,7 +81,7 @@ namespace Start.Server.Data.Services { return false; this.db.BookmarkGroups.Remove(bookmarkGroup); - this.db.SaveChanges(); + await this.db.SaveChangesAsync(); return true; } diff --git a/Start/Server/Data/Services/BookmarkService.cs b/Start/Server/Data/Services/BookmarkService.cs index 691b4da..078d470 100644 --- a/Start/Server/Data/Services/BookmarkService.cs +++ b/Start/Server/Data/Services/BookmarkService.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Start.Server.Data.Services.Interfaces; using Start.Server.Models; @@ -12,29 +13,29 @@ namespace Start.Server.Data.Services { this.db = dbContext; } - public Bookmark? GetBookmark(string userId, int bookmarkId) { + public async Task GetBookmark(string userId, int bookmarkId) { if (!BookmarkOwnershipTools.IsBookmarkOwner(this.db, userId, bookmarkId)) return null; - return this.db.Bookmarks - .SingleOrDefault(b => b.BookmarkId == bookmarkId); + return await this.db.Bookmarks + .SingleOrDefaultAsync(b => b.BookmarkId == bookmarkId); } - public IList GetUserBookmarks(string userId) { - return this.db.Bookmarks + public async Task> GetUserBookmarks(string userId) { + return await this.db.Bookmarks .Where(b => b.BookmarkGroup!.BookmarkContainer!.ApplicationUserId == userId) - .ToList(); + .ToListAsync(); } - public Bookmark? CreateBookmark(string userId, string title, string url, string? notes, + public async Task CreateBookmark(string userId, string title, string url, string? notes, int bookmarkGroupId) { if (!BookmarkOwnershipTools.IsBookmarkGroupOwner(this.db, userId, bookmarkGroupId)) return null; Bookmark newBookmark = new(title, url, bookmarkGroupId); - db.Bookmarks.Add(newBookmark); - db.SaveChanges(); + await db.Bookmarks.AddAsync(newBookmark); + await db.SaveChangesAsync(); if (newBookmark.BookmarkId <= 0) return null; @@ -42,7 +43,7 @@ namespace Start.Server.Data.Services { return newBookmark; } - public Bookmark? UpdateBookmark(string userId, Bookmark bookmark) { + public async Task UpdateBookmark(string userId, Bookmark bookmark) { Bookmark? existingBookmark = db.Bookmarks .SingleOrDefault(b => b.BookmarkId == bookmark.BookmarkId); @@ -55,12 +56,12 @@ namespace Start.Server.Data.Services { return null; db.Entry(bookmark).State = EntityState.Modified; - db.SaveChanges(); + await db.SaveChangesAsync(); return bookmark; } - public bool DeleteBookmark(string userId, int bookmarkId) { + public async Task DeleteBookmark(string userId, int bookmarkId) { Bookmark? bookmark = db.Bookmarks .SingleOrDefault(b => b.BookmarkId == bookmarkId); @@ -71,7 +72,7 @@ namespace Start.Server.Data.Services { return false; db.Bookmarks.Remove(bookmark); - db.SaveChanges(); + await db.SaveChangesAsync(); return true; } diff --git a/Start/Server/Data/Services/Interfaces/IBookmarkContainerService.cs b/Start/Server/Data/Services/Interfaces/IBookmarkContainerService.cs index f301d15..becc160 100644 --- a/Start/Server/Data/Services/Interfaces/IBookmarkContainerService.cs +++ b/Start/Server/Data/Services/Interfaces/IBookmarkContainerService.cs @@ -1,19 +1,18 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; +using System.Threading.Tasks; using Start.Server.Models; -using Start.Shared; namespace Start.Server.Data.Services.Interfaces { public interface IBookmarkContainerService { - public BookmarkContainer? GetBookmarkContainer(string userId, + public Task GetBookmarkContainer(string userId, int bookmarkContainerId, bool includeGroups = false, bool includeBookmarks = false); - public IList GetUserBookmarkContainers(string userId, + public Task> GetUserBookmarkContainers(string userId, bool includeGroups = false, bool includeBookmarks = false); - public BookmarkContainer? CreateBookmarkContainer(string userId, + public Task CreateBookmarkContainer(string userId, string title); - public BookmarkContainer? UpdateBookmarkContainer(string userId, + public Task UpdateBookmarkContainer(string userId, BookmarkContainer bookmarkContainer); - public bool DeleteBookmarkContainer(string userId, int bookmarkContainerId); + public Task DeleteBookmarkContainer(string userId, int bookmarkContainerId); } } diff --git a/Start/Server/Data/Services/Interfaces/IBookmarkGroupService.cs b/Start/Server/Data/Services/Interfaces/IBookmarkGroupService.cs index efffbe3..3ef1f20 100644 --- a/Start/Server/Data/Services/Interfaces/IBookmarkGroupService.cs +++ b/Start/Server/Data/Services/Interfaces/IBookmarkGroupService.cs @@ -1,19 +1,20 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; using Start.Server.Models; using Start.Shared; namespace Start.Server.Data.Services.Interfaces { public interface IBookmarkGroupService { - public BookmarkGroup? GetBookmarkGroup(string userId, + public Task GetBookmarkGroup(string userId, int bookmarkGroupId, bool includeBookmarks = false); - public IList GetUserBookmarkGroups(string userId, + public Task> GetUserBookmarkGroups(string userId, bool includeBookmarks = false); - public BookmarkGroup? CreateBookmarkGroup(string userId, string title, + public Task CreateBookmarkGroup(string userId, string title, string color, int bookmarkContainerId); - public BookmarkGroup? UpdateBookmarkGroup(string userId, + public Task UpdateBookmarkGroup(string userId, BookmarkGroup bookmarkGroup); - public bool DeleteBookmarkGroup(string userId, int bookmarkGroupId); + public Task DeleteBookmarkGroup(string userId, int bookmarkGroupId); } } diff --git a/Start/Server/Data/Services/Interfaces/IBookmarkService.cs b/Start/Server/Data/Services/Interfaces/IBookmarkService.cs index 631fb08..b7da850 100644 --- a/Start/Server/Data/Services/Interfaces/IBookmarkService.cs +++ b/Start/Server/Data/Services/Interfaces/IBookmarkService.cs @@ -1,16 +1,15 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; +using System.Threading.Tasks; using Start.Server.Models; -using Start.Shared; namespace Start.Server.Data.Services.Interfaces { public interface IBookmarkService { - public Bookmark? GetBookmark(string userId, int bookmarkId); - public IList GetUserBookmarks(string userId); + public Task GetBookmark(string userId, int bookmarkId); + public Task> GetUserBookmarks(string userId); - public Bookmark? CreateBookmark(string userId, string title, string url, + public Task CreateBookmark(string userId, string title, string url, string? notes, int bookmarkGroupId); - public Bookmark? UpdateBookmark(string userId, Bookmark bookmark); - public bool DeleteBookmark(string userId, int bookmarkId); + public Task UpdateBookmark(string userId, Bookmark bookmark); + public Task DeleteBookmark(string userId, int bookmarkId); } } diff --git a/Start/Server/Startup.cs b/Start/Server/Startup.cs index 48eba48..e918000 100644 --- a/Start/Server/Startup.cs +++ b/Start/Server/Startup.cs @@ -9,6 +9,8 @@ using Start.Server.Data; using Start.Server.Models; using Start.Server.Data.Services; using Start.Server.Data.Services.Interfaces; +using Refit; +using Start.Shared.Api; namespace Start.Server { public class Startup { diff --git a/Start/Shared/Api/IBookmarkContainersApi.cs b/Start/Shared/Api/IBookmarkContainersApi.cs new file mode 100644 index 0000000..054ba47 --- /dev/null +++ b/Start/Shared/Api/IBookmarkContainersApi.cs @@ -0,0 +1,21 @@ +using System.Collections.Generic; +using System.Net.Http; +using System.Threading.Tasks; +using Refit; + +namespace Start.Shared.Api { + public interface IBookmarkContainersApi { + [Get("/")] + Task>> GetAllBookmarkContainers(); + + [Get("/{bookmarkContainerId}")] + Task> GetBookmarkContainer(int bookmarkContainerId); + + [Post("/Create")] + Task> CreateBookmarkContainer( + [Body(BodySerializationMethod.Serialized)] string title); + + [Delete("/Delete/{bookmarkContainerId}")] + Task DeleteBookmarkContainer(int bookmarkContainerId); + } +} diff --git a/Start/Shared/Api/IBookmarksApi.cs b/Start/Shared/Api/IBookmarksApi.cs new file mode 100644 index 0000000..b56aa6a --- /dev/null +++ b/Start/Shared/Api/IBookmarksApi.cs @@ -0,0 +1,15 @@ +using System.Threading.Tasks; +using Refit; + +namespace Start.Shared.Api { + public interface IBookmarksApi { + [Get("{bookmarkId}")] + Task GetBookmark(int bookmarkId); + + [Post("/Create")] + Task CreateBookmark(string title, string url, string? notes, int bookmarkGroupId); + + [Delete("/Delete/{bookmarkId}")] + Task DeleteBookmark(int bookmarkId); + } +} diff --git a/Start/Shared/Start.Shared.csproj b/Start/Shared/Start.Shared.csproj index 0cb6941..3aafd8a 100644 --- a/Start/Shared/Start.Shared.csproj +++ b/Start/Shared/Start.Shared.csproj @@ -9,4 +9,14 @@ + + + + + + + + + +