diff --git a/WF_EF_Api/Contextlib/DbSourceManager.cs b/WF_EF_Api/Contextlib/DbSourceManager.cs index 6f470f7..e2ed299 100644 --- a/WF_EF_Api/Contextlib/DbSourceManager.cs +++ b/WF_EF_Api/Contextlib/DbSourceManager.cs @@ -5,6 +5,7 @@ using System.Text; using System.Threading.Tasks; using Entity; using Shared; +using static System.Runtime.InteropServices.JavaScript.JSType; namespace Contextlib { @@ -40,6 +41,12 @@ namespace Contextlib throw new NotImplementedException(); } + public async Task> GetSomesSource(int page, int count) + { + var srcLst = _repo.GetItems(page, count, []).ToList(); + return new PaginationResult(srcLst.Count, 0, srcLst.Count, srcLst); + } + public async Task> GetSourceByDate(int date) { diff --git a/WF_EF_Api/ServicesApi/SourceService.cs b/WF_EF_Api/ServicesApi/SourceService.cs index 217c2ad..acefbcc 100644 --- a/WF_EF_Api/ServicesApi/SourceService.cs +++ b/WF_EF_Api/ServicesApi/SourceService.cs @@ -36,6 +36,12 @@ namespace ServicesApi return await srcService.GetLastSourceId(); } + public async Task> GetSomesSource(int page, int count) + { + var sources = (await srcService.GetSomesSource(page, count)).items; + return new PaginationResult(sources.Count(), page, count, sources.ToDto()); + } + public async Task> GetSourceByDate(int date) { var sources = (await srcService.GetSourceByDate(date)).items; @@ -44,7 +50,7 @@ namespace ServicesApi public async Task GetSourceById(int id) { - return srcService.GetSourceById(id).Result.ToDto(); + return (await srcService.GetSourceById(id)).ToDto(); } public async Task GetSourceByTitle(string title) diff --git a/WF_EF_Api/Shared/ISourceService.cs b/WF_EF_Api/Shared/ISourceService.cs index 9657db7..73fd2c6 100644 --- a/WF_EF_Api/Shared/ISourceService.cs +++ b/WF_EF_Api/Shared/ISourceService.cs @@ -42,5 +42,7 @@ namespace Shared // Retrieves the unique identifier of the last added source. Task GetLastSourceId(); + + Task> GetSomesSource(int page, int count); } } diff --git a/WF_EF_Api/WfApi/Controllers/SourceController.cs b/WF_EF_Api/WfApi/Controllers/SourceController.cs index 770a55f..744a737 100644 --- a/WF_EF_Api/WfApi/Controllers/SourceController.cs +++ b/WF_EF_Api/WfApi/Controllers/SourceController.cs @@ -1,5 +1,6 @@ using System.Net; using DTO; +using Entity; using Microsoft.AspNetCore.Mvc; using Shared; @@ -43,22 +44,59 @@ namespace WfApi.Controllers } } - /*[HttpGet("all")] + [HttpGet("all")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task GetAllSource(int index = 0, int count = 10) { + try + { + var result = await _source.GetSomesSource(index, count); + if (result != null) + { + return await Task.FromResult(Ok(result)); + } + else + { + return NoContent(); + } + } + catch (Exception e) + { + return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error (" + e + ")" }); + } } [HttpPost] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status500InternalServerError)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] public async Task CreateSource([FromBody] SourceDTO newSource) { - + try + { + if(newSource == null) + { + return BadRequest(new { message = "Source data is required." }); + } + try + { + var existingSource = await _source.GetSourceById(newSource.Id); + return Conflict(new { message = "A source with this ID already exists." }); + } + catch(KeyNotFoundException e) + { + await _source.AddSource(newSource); + return CreatedAtAction(nameof(GetAllSource), new { id = newSource.Id }, newSource); + } + } + catch (Exception e) + { + return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error (" + e + ")" }); + } } [HttpPut()] @@ -67,16 +105,21 @@ namespace WfApi.Controllers [ProducesResponseType(StatusCodes.Status500InternalServerError)] public async Task UpdateSource([FromQuery] int id, [FromBody] SourceDTO updatedSource) { + try + { + if (updatedSource == null) + { + return BadRequest(new { message = "new source data is required." }); + } - } - - [HttpDelete("delete")] - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status204NoContent)] - [ProducesResponseType(StatusCodes.Status500InternalServerError)] - public async Task DeleteSource([FromQuery] int idSource) - { + var result = _source.UpdateSource(id, updatedSource); - }*/ + return Ok(result); + } + catch (Exception e) + { + return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error (" + e + ")" }); + } + } } } diff --git a/WF_EF_Api/WfApi/Program.cs b/WF_EF_Api/WfApi/Program.cs index 24338f1..71bbd5b 100644 --- a/WF_EF_Api/WfApi/Program.cs +++ b/WF_EF_Api/WfApi/Program.cs @@ -26,6 +26,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped, DbUsersManager>(); builder.Services.AddScoped, DbQuoteManager>(); +builder.Services.AddScoped, DbFavoriteManager>(); builder.Services.AddScoped, DbCommentaryManager>(); builder.Services.AddScoped, DbCharacterManager>(); builder.Services.AddScoped, DbImagesManager>();