Files
kemkas-gh-mirror/Kemkas.Web/Services/Character/CharacterPersistenceService.cs
T
morbalint 7bb7ab6f13 adding hidden basic asp.net core backend (#30)
Base functionality:

    DB
    email sending
    user registration & login
    storing new characters and reading them back

Notes:

    Functionality is "hidden" with no links pointing to it.
    Most pages are scaffolded and not reviewed at all :(
    local Dev db setup is pushed to git
    NO local email setup :(
2023-12-19 19:03:57 +01:00

47 lines
1.4 KiB
C#

using Kemkas.Web.Db;
using Kemkas.Web.Db.Models;
using Kemkas.Web.Services.Identity;
using Microsoft.EntityFrameworkCore;
namespace Kemkas.Web.Services.Character;
public interface ICharacterPersistenceService
{
public Task<Guid> StoreNewCharacter(V1Karakter karakter);
public Task<V1Karakter?> GetCharacterById(Guid id);
}
public class CharacterPersistenceService(
ApplicationDbContext dbContext,
ICurrentUserService currentUserService
) : ICharacterPersistenceService
{
public async Task<Guid> StoreNewCharacter(V1Karakter karakter)
{
var currentUser = await currentUserService.GetCurrentUser();
karakter.OwnerUserId = currentUser?.Id;
karakter.Id = Guid.Empty;
dbContext.Karakterek.Add(karakter);
await dbContext.SaveChangesAsync();
return karakter.Id;
}
public async Task<V1Karakter?> GetCharacterById(Guid id)
{
var entity = await dbContext.Karakterek
.Include(x => x.KarakterKepzettsegek)
.Include(x => x.Szintlepesek)
.Include(x => x.Felszereles)
.FirstOrDefaultAsync(x => x.Id == id);
if (entity?.OwnerUserId is null || entity.IsPublic)
{
return entity;
}
var currentUser = await currentUserService.GetCurrentUser();
return entity.OwnerUserId == currentUser?.Id ? entity : null;
}
}