Fin route Source

pull/6/head^2
Kevin MONDEJAR 3 weeks ago
parent f52bd3aced
commit 3da9d718e7

@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using Entity; using Entity;
using Shared; using Shared;
using static System.Runtime.InteropServices.JavaScript.JSType;
namespace Contextlib namespace Contextlib
{ {
@ -40,6 +41,12 @@ namespace Contextlib
throw new NotImplementedException(); throw new NotImplementedException();
} }
public async Task<PaginationResult<Source>> GetSomesSource(int page, int count)
{
var srcLst = _repo.GetItems(page, count, []).ToList();
return new PaginationResult<Source>(srcLst.Count, 0, srcLst.Count, srcLst);
}
public async Task<PaginationResult<Source>> GetSourceByDate(int date) public async Task<PaginationResult<Source>> GetSourceByDate(int date)
{ {

@ -36,6 +36,12 @@ namespace ServicesApi
return await srcService.GetLastSourceId(); return await srcService.GetLastSourceId();
} }
public async Task<PaginationResult<SourceDTO>> GetSomesSource(int page, int count)
{
var sources = (await srcService.GetSomesSource(page, count)).items;
return new PaginationResult<SourceDTO>(sources.Count(), page, count, sources.ToDto());
}
public async Task<PaginationResult<SourceDTO>> GetSourceByDate(int date) public async Task<PaginationResult<SourceDTO>> GetSourceByDate(int date)
{ {
var sources = (await srcService.GetSourceByDate(date)).items; var sources = (await srcService.GetSourceByDate(date)).items;
@ -44,7 +50,7 @@ namespace ServicesApi
public async Task<SourceDTO> GetSourceById(int id) public async Task<SourceDTO> GetSourceById(int id)
{ {
return srcService.GetSourceById(id).Result.ToDto(); return (await srcService.GetSourceById(id)).ToDto();
} }
public async Task<SourceDTO> GetSourceByTitle(string title) public async Task<SourceDTO> GetSourceByTitle(string title)

@ -42,5 +42,7 @@ namespace Shared
// Retrieves the unique identifier of the last added source. // Retrieves the unique identifier of the last added source.
Task<int> GetLastSourceId(); Task<int> GetLastSourceId();
Task<PaginationResult<TSource>> GetSomesSource(int page, int count);
} }
} }

@ -1,5 +1,6 @@
using System.Net; using System.Net;
using DTO; using DTO;
using Entity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Shared; using Shared;
@ -43,22 +44,59 @@ namespace WfApi.Controllers
} }
} }
/*[HttpGet("all")] [HttpGet("all")]
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)] [ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> GetAllSource(int index = 0, int count = 10) public async Task<IActionResult> GetAllSource(int index = 0, int count = 10)
{ {
try
{
var result = await _source.GetSomesSource(index, count);
if (result != null)
{
return await Task.FromResult<IActionResult>(Ok(result));
}
else
{
return NoContent();
}
}
catch (Exception e)
{
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error (" + e + ")" });
}
} }
[HttpPost] [HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)] [ProducesResponseType(StatusCodes.Status500InternalServerError)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<IActionResult> CreateSource([FromBody] SourceDTO newSource) public async Task<IActionResult> 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()] [HttpPut()]
@ -67,16 +105,21 @@ namespace WfApi.Controllers
[ProducesResponseType(StatusCodes.Status500InternalServerError)] [ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> UpdateSource([FromQuery] int id, [FromBody] SourceDTO updatedSource) public async Task<IActionResult> UpdateSource([FromQuery] int id, [FromBody] SourceDTO updatedSource)
{ {
try
{
if (updatedSource == null)
{
return BadRequest(new { message = "new source data is required." });
} }
[HttpDelete("delete")] var result = _source.UpdateSource(id, updatedSource);
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> DeleteSource([FromQuery] int idSource)
{
}*/ return Ok(result);
}
catch (Exception e)
{
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error (" + e + ")" });
}
}
} }
} }

@ -26,6 +26,7 @@ builder.Services.AddScoped<WTFContext, StubWTFContext>();
builder.Services.AddScoped<IUserService<Users>, DbUsersManager>(); builder.Services.AddScoped<IUserService<Users>, DbUsersManager>();
builder.Services.AddScoped<IQuoteService<Quote>, DbQuoteManager>(); builder.Services.AddScoped<IQuoteService<Quote>, DbQuoteManager>();
builder.Services.AddScoped<IFavoriteService<Quote>, DbFavoriteManager>();
builder.Services.AddScoped<ICommentaryService<Commentary>, DbCommentaryManager>(); builder.Services.AddScoped<ICommentaryService<Commentary>, DbCommentaryManager>();
builder.Services.AddScoped<ICharacterService<Character>, DbCharacterManager>(); builder.Services.AddScoped<ICharacterService<Character>, DbCharacterManager>();
builder.Services.AddScoped<IImagesService<Images>, DbImagesManager>(); builder.Services.AddScoped<IImagesService<Images>, DbImagesManager>();

Loading…
Cancel
Save