Skip to content

Commit

Permalink
feat: 2e save character initial implementation WIP
Browse files Browse the repository at this point in the history
2e is hidden behind url navigation hiding, so hoping that nobody found it yet.

Note: DB migration make this commit a bit risky!
  • Loading branch information
morbalint committed Feb 14, 2024
1 parent 8b3fa2f commit 41e894c
Show file tree
Hide file tree
Showing 39 changed files with 1,987 additions and 365 deletions.
90 changes: 90 additions & 0 deletions Kemkas.Web/Controllers/Character1EController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System.ComponentModel.DataAnnotations;
using Kemkas.Web.Db.Models;
using Kemkas.Web.Services.FirstEdition.Character;
using Kemkas.Web.Services.Shared;
using Kemkas.Web.ViewModels;
using Microsoft.AspNetCore.Mvc;

namespace Kemkas.Web.Controllers;

/// <summary>
/// First edition character endpoints
/// </summary>
/// <param name="validationService"></param>
/// <param name="dtoToDbModelService"></param>
/// <param name="dbModelToDtoService"></param>
/// <param name="persistenceService"></param>
[ApiController]
[Route("[controller]")]
public class Character1EController(
ICharacterValidationService validationService,
ICharacter1EDtoToDbModelService dtoToDbModelService,
ICharacterDbModelToDtoService dbModelToDtoService,
ICharacterPersistenceService persistenceService)
: ControllerBase
{
[HttpPost]
public async Task<ActionResult<Guid>> StoreNewCharacter([FromBody] Character1eDto dto, [FromQuery]bool isPublic = true)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var errors = validationService.Validate(dto);
if (errors != null)
{
return BadRequest(errors);
}

V1Karakter model;
try
{
model = dtoToDbModelService.Convert(dto);
}
catch (ValidationException ex)
{
return BadRequest(ex.Message);
}
var id = await persistenceService.StoreNewCharacter1E(model, isPublic);
return id;
}

[HttpGet("{id:guid}")]
public async Task<ActionResult<Character1eDto>> GetCharacterById([FromRoute] Guid id)
{
var entity = await persistenceService.GetCharacter1EById(id);
if (entity is null)
{
return NotFound();
}

return dbModelToDtoService.Convert(entity);
}

[HttpPost("{id:guid}")]
public async Task<ActionResult<Guid>> UpdateCharacter([FromRoute] Guid id, [FromBody] Character1eDto dto, [FromQuery]bool isPublic = true)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var errors = validationService.Validate(dto);
if (errors != null)
{
return BadRequest(errors);
}

var original = await persistenceService.GetCharacter1EById(id, tracking: true);
if (original is null)
{
return NotFound();
}

dtoToDbModelService.Update(original, dto);

await persistenceService.UpdateCharacter1E(original, isPublic);

return id;
}

}
43 changes: 43 additions & 0 deletions Kemkas.Web/Controllers/Character2EController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.ComponentModel.DataAnnotations;
using Kemkas.Web.Db.Models;
using Kemkas.Web.Services.SecondEdition.Character;
using Kemkas.Web.Services.Shared;
using Kemkas.Web.ViewModels;
using Microsoft.AspNetCore.Mvc;

namespace Kemkas.Web.Controllers;

[ApiController]
[Route("[controller]")]
public class Character2EController(
ICharacter2EDtoToDbModelService dtoToDbModelService,
ICharacterPersistenceService persistenceService
) : ControllerBase
{
[HttpPost]
public async Task<ActionResult<Guid>> StoreNewCharacter([FromBody] Character2eDto dto, [FromQuery]bool isPublic = true)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// var errors = validationService.Validate(dto);
// if (errors != null)
// {
// return BadRequest(errors);
// }

V2Karakter model;
try
{
model = dtoToDbModelService.Convert(dto);
}
catch (ValidationException ex)
{
return BadRequest(ex.Message);
}
var id = await persistenceService.StoreNewCharacter2E(model, isPublic);
return id;
}

}
80 changes: 7 additions & 73 deletions Kemkas.Web/Controllers/CharacterController.cs
Original file line number Diff line number Diff line change
@@ -1,85 +1,19 @@
using System.ComponentModel.DataAnnotations;
using Kemkas.Web.Db.Models;
using Kemkas.Web.Services.Character;
using Kemkas.Web.Services.FirstEdition.Character;
using Kemkas.Web.Services.Shared;
using Kemkas.Web.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Kemkas.Web.Controllers;

/// <summary>
/// Shared first and second edition character endpoints
/// </summary>
/// <param name="persistenceService"></param>
[ApiController]
[Route("[controller]")]
public class CharacterController(
ICharacterValidationService validationService,
ICharacterDtoToDbModelService dtoToDbModelService,
ICharacterDbModelToDtoService dbModelToDtoService,
ICharacterPersistenceService persistenceService)
: ControllerBase
public class CharacterController(ICharacterPersistenceService persistenceService) : ControllerBase
{
[HttpPost]
public async Task<ActionResult<Guid>> StoreNewCharacter([FromBody] CharacterDto dto, [FromQuery]bool isPublic = true)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var errors = validationService.Validate(dto);
if (errors != null)
{
return BadRequest(errors);
}

V1Karakter model;
try
{
model = dtoToDbModelService.Convert(dto);
}
catch (ValidationException ex)
{
return BadRequest(ex.Message);
}
var id = await persistenceService.StoreNewCharacter(model, isPublic);
return id;
}

[HttpGet("{id:guid}")]
public async Task<ActionResult<CharacterDto>> GetCharacterById([FromRoute] Guid id)
{
var entity = await persistenceService.GetCharacterById(id);
if (entity is null)
{
return NotFound();
}

return dbModelToDtoService.Convert(entity);
}

[HttpPost("{id:guid}")]
public async Task<ActionResult<Guid>> UpdateCharacter([FromRoute] Guid id, [FromBody] CharacterDto dto, [FromQuery]bool isPublic = true)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var errors = validationService.Validate(dto);
if (errors != null)
{
return BadRequest(errors);
}

var original = await persistenceService.GetCharacterById(id, tracking: true);
if (original is null)
{
return NotFound();
}

dtoToDbModelService.Update(original, dto);

await persistenceService.UpdateCharacter(original, isPublic);

return id;
}

[HttpGet]
[Authorize]
public async Task<List<CharacterListItemDto>> GetAllCharacters()
Expand Down
35 changes: 0 additions & 35 deletions Kemkas.Web/Controllers/WeatherForecastController.cs

This file was deleted.

11 changes: 11 additions & 0 deletions Kemkas.Web/Db/ApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public ApplicationDbContext(DbContextOptions options) : base(options)

public DbSet<DataProtectionKey> DataProtectionKeys { get; set; }

// 1st edition character

public DbSet<V1Karakter> Karakterek { get; set; }

public DbSet<V1KarakterKepzettseg> KarakterKepzettsegek { get; set; }
Expand All @@ -21,4 +23,13 @@ public ApplicationDbContext(DbContextOptions options) : base(options)

public DbSet<V1Felszereles> Felszerelesek { get; set; }

// 2nd edition character

public DbSet<V2Karakter> Karakterek2E { get; set; }

public DbSet<V2KarakterKepzettseg> KarakterKepzettsegek2E { get; set; }

public DbSet<V2Szintlepes> Szintlepesek2E { get; set; }

public DbSet<V2Felszereles> Felszerelesek2E { get; set; }
}
21 changes: 19 additions & 2 deletions Kemkas.Web/Db/Enums/Faj.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Kemkas.Web.Db.Enums;

public enum Faj
public enum Faj1E
{
Ember = 0,
Amazon = 1,
Expand All @@ -14,4 +14,21 @@ public enum Faj
Felszerzet = 9,
Gnom = 10,
Torpe = 11,
}
}

public enum Faj2E
{
Ember = 0,
Amazon = 1,
Birodalmi = 2,
Nomad = 3,
Eszaki = 4,
Osember = 5,
Elf = 6,
Felelf = 7,
Felork = 8,
Felszerzet = 9,
Gnom = 10,
Torpe = 11,
}

Loading

0 comments on commit 41e894c

Please sign in to comment.