From 5f2f5055ad41cb88bba168dcf3ccbefdd5d05d50 Mon Sep 17 00:00:00 2001 From: cuqmbr Date: Sun, 17 Jul 2022 14:32:53 +0300 Subject: [PATCH] feat: integrate DTOs to api --- Server/Controllers/ScoreboardController.cs | 28 +++++++-------- Server/Data/ServerDbContext.cs | 2 +- Server/Services/ScoreboardService.cs | 42 +++++++++++++++++----- 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/Server/Controllers/ScoreboardController.cs b/Server/Controllers/ScoreboardController.cs index 5950b6e..0d978be 100644 --- a/Server/Controllers/ScoreboardController.cs +++ b/Server/Controllers/ScoreboardController.cs @@ -1,8 +1,6 @@ -using System.Data; -using DatabaseModels.Plain; +using DatabaseModels.DataTransferObjets; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; -using Server.Data; using Server.Services; namespace Server.Controllers; @@ -20,47 +18,47 @@ public class ScoreboardController : ControllerBase // GET: /scoreboard [HttpGet] - public async Task> Get() + public async Task> Get() { return await _sbService.GetScoreboard(); } // GET: /scoreboard/cuqmbr [HttpGet("{username}", Name = "Get")] - public async Task> Get(string username) + public async Task> Get(string username) { - var sbRecord = await _sbService.GetUserHighScore(username); + var sbRecordDto = await _sbService.GetUserHighScore(username); - if (sbRecord == null) + if (sbRecordDto == null) { return NotFound(); } - return sbRecord; + return sbRecordDto; } // POST: /scoreboard [HttpPost] - public async Task Post([FromBody] ScoreboardRecord sbRecord) + public async Task Post([FromBody] ScoreboardRecordDto sbRecordDto) { - await _sbService.AddUserHighScore(sbRecord); - return CreatedAtAction(nameof(Get), new {sbRecord.User}, sbRecord); + await _sbService.AddUserHighScore(sbRecordDto); + return CreatedAtAction(nameof(Get), new {sbRecordDto}, sbRecordDto); } // PUT: /scoreboard/id [HttpPut("{id}", Name = "Put")] - public async Task> Put(int id, [FromBody] ScoreboardRecord sbRecord) + public async Task Put(int id, [FromBody] ScoreboardRecordDto sbRecordDto) { - if (id != sbRecord.Id) + if (id != sbRecordDto.Id) { return BadRequest(); } try { - await _sbService.UpdateScoreboardRecord(sbRecord); + await _sbService.UpdateScoreboardRecord(sbRecordDto); } - catch (DBConcurrencyException) + catch (DbUpdateConcurrencyException) { if (!await _sbService.ScoreboardRecordExists(id)) { diff --git a/Server/Data/ServerDbContext.cs b/Server/Data/ServerDbContext.cs index 13285a0..3be10bb 100644 --- a/Server/Data/ServerDbContext.cs +++ b/Server/Data/ServerDbContext.cs @@ -1,4 +1,4 @@ -using DatabaseModels.Plain; +using DatabaseModels.InitialObjects; using Microsoft.EntityFrameworkCore; namespace Server.Data; diff --git a/Server/Services/ScoreboardService.cs b/Server/Services/ScoreboardService.cs index 61deced..4511dc9 100644 --- a/Server/Services/ScoreboardService.cs +++ b/Server/Services/ScoreboardService.cs @@ -1,4 +1,5 @@ -using DatabaseModels.Plain; +using DatabaseModels.DataTransferObjets; +using DatabaseModels.InitialObjects; using Microsoft.EntityFrameworkCore; using Server.Data; @@ -15,42 +16,65 @@ public class ScoreboardService // GET - public async Task GetScoreboard() + public async Task GetScoreboard() { var sbRecords = await _dbContext.Scoreboard .Include(sbr => sbr.User) .ToListAsync(); 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(sbRecords.Count); + + foreach (var sbr in sbRecords) + { + dto.Add(sbr.ToDto()); + } + + return dto.ToArray(); } - public async Task GetUserHighScore(string username) + public async Task GetUserHighScore(string username) { var userScoreboardRecords = await _dbContext.Scoreboard .Include(sbr => sbr.User) .Where(sbr => sbr.User.Username == username) .ToListAsync(); - - return userScoreboardRecords.MaxBy(sbr => sbr.Score); + + return userScoreboardRecords.MaxBy(sbr => sbr.Score)?.ToDto(); } // 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; await _dbContext.AddAsync(sbRecord); await _dbContext.SaveChangesAsync(); + + sbRecordDto.Id = _dbContext.ChangeTracker.Entries().First().Entity.Id; } // 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; await _dbContext.SaveChangesAsync(); }