feat: integrate DTOs to api
This commit is contained in:
parent
c8bc5f2989
commit
5f2f5055ad
@ -1,8 +1,6 @@
|
|||||||
using System.Data;
|
using DatabaseModels.DataTransferObjets;
|
||||||
using DatabaseModels.Plain;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Server.Data;
|
|
||||||
using Server.Services;
|
using Server.Services;
|
||||||
|
|
||||||
namespace Server.Controllers;
|
namespace Server.Controllers;
|
||||||
@ -20,47 +18,47 @@ public class ScoreboardController : ControllerBase
|
|||||||
|
|
||||||
// GET: /scoreboard
|
// GET: /scoreboard
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult<ScoreboardRecord[]>> Get()
|
public async Task<ActionResult<ScoreboardRecordDto[]>> Get()
|
||||||
{
|
{
|
||||||
return await _sbService.GetScoreboard();
|
return await _sbService.GetScoreboard();
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET: /scoreboard/cuqmbr
|
// GET: /scoreboard/cuqmbr
|
||||||
[HttpGet("{username}", Name = "Get")]
|
[HttpGet("{username}", Name = "Get")]
|
||||||
public async Task<ActionResult<ScoreboardRecord>> Get(string username)
|
public async Task<ActionResult<ScoreboardRecordDto>> Get(string username)
|
||||||
{
|
{
|
||||||
var sbRecord = await _sbService.GetUserHighScore(username);
|
var sbRecordDto = await _sbService.GetUserHighScore(username);
|
||||||
|
|
||||||
if (sbRecord == null)
|
if (sbRecordDto == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
return NotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
return sbRecord;
|
return sbRecordDto;
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST: /scoreboard
|
// POST: /scoreboard
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<ActionResult> Post([FromBody] ScoreboardRecord sbRecord)
|
public async Task<ActionResult> Post([FromBody] ScoreboardRecordDto sbRecordDto)
|
||||||
{
|
{
|
||||||
await _sbService.AddUserHighScore(sbRecord);
|
await _sbService.AddUserHighScore(sbRecordDto);
|
||||||
return CreatedAtAction(nameof(Get), new {sbRecord.User}, sbRecord);
|
return CreatedAtAction(nameof(Get), new {sbRecordDto}, sbRecordDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
// PUT: /scoreboard/id
|
// PUT: /scoreboard/id
|
||||||
[HttpPut("{id}", Name = "Put")]
|
[HttpPut("{id}", Name = "Put")]
|
||||||
public async Task<ActionResult<ScoreboardRecord>> Put(int id, [FromBody] ScoreboardRecord sbRecord)
|
public async Task<ActionResult> Put(int id, [FromBody] ScoreboardRecordDto sbRecordDto)
|
||||||
{
|
{
|
||||||
if (id != sbRecord.Id)
|
if (id != sbRecordDto.Id)
|
||||||
{
|
{
|
||||||
return BadRequest();
|
return BadRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _sbService.UpdateScoreboardRecord(sbRecord);
|
await _sbService.UpdateScoreboardRecord(sbRecordDto);
|
||||||
}
|
}
|
||||||
catch (DBConcurrencyException)
|
catch (DbUpdateConcurrencyException)
|
||||||
{
|
{
|
||||||
if (!await _sbService.ScoreboardRecordExists(id))
|
if (!await _sbService.ScoreboardRecordExists(id))
|
||||||
{
|
{
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
using DatabaseModels.Plain;
|
using DatabaseModels.InitialObjects;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace Server.Data;
|
namespace Server.Data;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using DatabaseModels.Plain;
|
using DatabaseModels.DataTransferObjets;
|
||||||
|
using DatabaseModels.InitialObjects;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Server.Data;
|
using Server.Data;
|
||||||
|
|
||||||
@ -15,42 +16,65 @@ public class ScoreboardService
|
|||||||
|
|
||||||
// GET
|
// GET
|
||||||
|
|
||||||
public async Task<ScoreboardRecord[]> GetScoreboard()
|
public async Task<ScoreboardRecordDto[]> GetScoreboard()
|
||||||
{
|
{
|
||||||
var sbRecords = await _dbContext.Scoreboard
|
var sbRecords = await _dbContext.Scoreboard
|
||||||
.Include(sbr => sbr.User)
|
.Include(sbr => sbr.User)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
sbRecords.Sort((a, b) => b.Score - a.Score);
|
sbRecords.Sort((a, b) => b.Score - a.Score);
|
||||||
|
sbRecords = sbRecords.DistinctBy(sbr => sbr.User.Id).ToList();
|
||||||
|
|
||||||
return sbRecords.DistinctBy(sbr => sbr.User.Id).ToArray();
|
var dto = new List<ScoreboardRecordDto>(sbRecords.Count);
|
||||||
|
|
||||||
|
foreach (var sbr in sbRecords)
|
||||||
|
{
|
||||||
|
dto.Add(sbr.ToDto());
|
||||||
|
}
|
||||||
|
|
||||||
|
return dto.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<ScoreboardRecord?> GetUserHighScore(string username)
|
public async Task<ScoreboardRecordDto?> GetUserHighScore(string username)
|
||||||
{
|
{
|
||||||
var userScoreboardRecords = await _dbContext.Scoreboard
|
var userScoreboardRecords = await _dbContext.Scoreboard
|
||||||
.Include(sbr => sbr.User)
|
.Include(sbr => sbr.User)
|
||||||
.Where(sbr => sbr.User.Username == username)
|
.Where(sbr => sbr.User.Username == username)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
return userScoreboardRecords.MaxBy(sbr => sbr.Score);
|
return userScoreboardRecords.MaxBy(sbr => sbr.Score)?.ToDto();
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST
|
// POST
|
||||||
|
|
||||||
public async Task AddUserHighScore(ScoreboardRecord sbRecord)
|
public async Task AddUserHighScore(ScoreboardRecordDto sbRecordDto)
|
||||||
{
|
{
|
||||||
var dbUser = await _dbContext.Users.FindAsync(sbRecord.User.Id);
|
var sbRecord = new ScoreboardRecord {
|
||||||
|
Score = sbRecordDto.Score,
|
||||||
|
PostTime = sbRecordDto.PostTime
|
||||||
|
};
|
||||||
|
var dbUser = await _dbContext.Users.FindAsync(sbRecordDto.User.Id);
|
||||||
sbRecord.User = dbUser;
|
sbRecord.User = dbUser;
|
||||||
|
|
||||||
await _dbContext.AddAsync(sbRecord);
|
await _dbContext.AddAsync(sbRecord);
|
||||||
await _dbContext.SaveChangesAsync();
|
await _dbContext.SaveChangesAsync();
|
||||||
|
|
||||||
|
sbRecordDto.Id = _dbContext.ChangeTracker.Entries<ScoreboardRecord>().First().Entity.Id;
|
||||||
}
|
}
|
||||||
|
|
||||||
// PUT
|
// PUT
|
||||||
|
|
||||||
public async Task UpdateScoreboardRecord(ScoreboardRecord sbRecord)
|
public async Task UpdateScoreboardRecord(ScoreboardRecordDto sbRecordDto)
|
||||||
{
|
{
|
||||||
|
var sbRecord = new ScoreboardRecord {
|
||||||
|
Id = sbRecordDto.Id,
|
||||||
|
Score = sbRecordDto.Score,
|
||||||
|
PostTime = sbRecordDto.PostTime,
|
||||||
|
User = new User {
|
||||||
|
Id = sbRecordDto.User.Id,
|
||||||
|
Username = sbRecordDto.User.Username
|
||||||
|
}
|
||||||
|
};
|
||||||
_dbContext.Entry(sbRecord).State = EntityState.Modified;
|
_dbContext.Entry(sbRecord).State = EntityState.Modified;
|
||||||
await _dbContext.SaveChangesAsync();
|
await _dbContext.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user