parent
409edd4199
commit
8a39e63211
@ -0,0 +1,121 @@
|
|||||||
|
using API_Services;
|
||||||
|
using Entities;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace API.Controllers;
|
||||||
|
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
[ApiController]
|
||||||
|
public class ArticleUserController : ControllerBase
|
||||||
|
{
|
||||||
|
|
||||||
|
private readonly IArticleUserService _us;
|
||||||
|
private readonly ILogger<ArticleUserController> _logger;
|
||||||
|
public ArticleUserController(IArticleUserService us, ILogger<ArticleUserController> logger)
|
||||||
|
{
|
||||||
|
this._us = us;
|
||||||
|
this._logger = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[HttpGet("/articleUsers")]
|
||||||
|
public async Task<IActionResult> GetAllArticleUsers()
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(GetAllArticleUsers), "");
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = await _us.GetAllArticleUsers();
|
||||||
|
if (result == null)
|
||||||
|
{
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
catch (Exception error)
|
||||||
|
{
|
||||||
|
_logger.LogError(error.Message);
|
||||||
|
return BadRequest(error.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("/user/{pseudo}/article")]
|
||||||
|
public async Task<IActionResult> GetArticleUser(string pseudo)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(GetArticleUser), pseudo);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = await _us.GetArticleUser(pseudo);
|
||||||
|
if (result == null)
|
||||||
|
{
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
catch (Exception error)
|
||||||
|
{
|
||||||
|
_logger.LogError(error.Message);
|
||||||
|
return BadRequest(error.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("/user/{pseudo}/article")]
|
||||||
|
public async Task<IActionResult> CreateArticleUser(ArticleUserEntity articleUser)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(CreateArticleUser), articleUser);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = await _us.CreateArticleUser(articleUser);
|
||||||
|
if (result == null)
|
||||||
|
{
|
||||||
|
return BadRequest($"ArticleUser {articleUser.UserEntityPseudo} already exists");
|
||||||
|
}
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
catch (Exception error)
|
||||||
|
{
|
||||||
|
_logger.LogError(error.Message);
|
||||||
|
return BadRequest(error.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete("/user/{pseudo}/article")]
|
||||||
|
public async Task<IActionResult> DeleteArticleUser(string pseudo)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(DeleteArticleUser), pseudo);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = await _us.DeleteArticleUser(pseudo);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
return BadRequest($"ArticleUser {pseudo} does not exist");
|
||||||
|
}
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
catch (Exception error)
|
||||||
|
{
|
||||||
|
_logger.LogError(error.Message);
|
||||||
|
return BadRequest(error.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut("/user/{pseudo}/article")]
|
||||||
|
public async Task<IActionResult> UpdateArticleUser(ArticleUserEntity articleUser)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(UpdateArticleUser), articleUser);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = await _us.UpdateArticleUser(articleUser);
|
||||||
|
if (!result)
|
||||||
|
{
|
||||||
|
return BadRequest($"ArticleUser {articleUser.UserEntityPseudo} does not exist");
|
||||||
|
}
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
|
catch (Exception error)
|
||||||
|
{
|
||||||
|
_logger.LogError(error.Message);
|
||||||
|
return BadRequest(error.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Binary file not shown.
@ -0,0 +1,67 @@
|
|||||||
|
using API_Services;
|
||||||
|
using DbContextLib;
|
||||||
|
using Entities;
|
||||||
|
using Model;
|
||||||
|
|
||||||
|
namespace DbDataManager;
|
||||||
|
|
||||||
|
public class DbManagerArticleUser : IArticleUserService
|
||||||
|
{
|
||||||
|
private readonly LibraryContext _context;
|
||||||
|
|
||||||
|
public DbManagerArticleUser(LibraryContext context)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<ArticleUserEntity?>> GetAllArticleUsers()
|
||||||
|
{
|
||||||
|
var entities = _context.ArticleUserSet.ToList();
|
||||||
|
if (entities == null) return await Task.FromResult<IEnumerable<ArticleUserEntity?>>(null);
|
||||||
|
return await Task.FromResult(entities.AsEnumerable());
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<ArticleUserEntity?> GetArticleUser(string pseudo)
|
||||||
|
{
|
||||||
|
var entity = _context.ArticleUserSet.FirstOrDefault(a => a.UserEntityPseudo.Equals(pseudo));
|
||||||
|
if (entity == null) return await Task.FromResult<ArticleUserEntity?>(null);
|
||||||
|
return await Task.FromResult(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public async Task<ArticleUserEntity?> CreateArticleUser(ArticleUserEntity articleUser)
|
||||||
|
{
|
||||||
|
var result = await GetArticleUser(articleUser.UserEntityPseudo);
|
||||||
|
if (result != null) return await Task.FromResult<ArticleUserEntity?>(null);
|
||||||
|
var entity = new ArticleUserEntity()
|
||||||
|
{
|
||||||
|
ArticleEntityId = articleUser.ArticleEntityId,
|
||||||
|
UserEntityPseudo = articleUser.UserEntityPseudo
|
||||||
|
};
|
||||||
|
if (entity == null) return await Task.FromResult<ArticleUserEntity?>(null);
|
||||||
|
_context.ArticleUserSet.Add(entity);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
return await Task.FromResult(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> DeleteArticleUser(string pseudo)
|
||||||
|
{
|
||||||
|
var entity = _context.ArticleUserSet.FirstOrDefault(a => a.UserEntityPseudo.Equals(pseudo));
|
||||||
|
if (entity == null) return await Task.FromResult(false);
|
||||||
|
_context.ArticleUserSet.Remove(entity);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
return await Task.FromResult(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> UpdateArticleUser(ArticleUserEntity articleUser)
|
||||||
|
{
|
||||||
|
var entity = _context.ArticleUserSet.FirstOrDefault(a => a.UserEntityPseudo.Equals(articleUser.UserEntityPseudo));
|
||||||
|
if (entity == null) return await Task.FromResult(false);
|
||||||
|
entity.ArticleEntityId = articleUser.ArticleEntityId;
|
||||||
|
entity.UserEntityPseudo = articleUser.UserEntityPseudo;
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
return await Task.FromResult(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
using Entities;
|
||||||
|
|
||||||
|
namespace API_Services;
|
||||||
|
|
||||||
|
public interface IArticleUserService
|
||||||
|
{
|
||||||
|
Task<IEnumerable<ArticleUserEntity>> GetAllArticleUsers();
|
||||||
|
Task<ArticleUserEntity?> GetArticleUser(string pseudo);
|
||||||
|
|
||||||
|
Task<ArticleUserEntity?> CreateArticleUser(ArticleUserEntity articleUser);
|
||||||
|
|
||||||
|
Task<bool> DeleteArticleUser(string pseudo);
|
||||||
|
|
||||||
|
Task<bool> UpdateArticleUser(ArticleUserEntity articleUser);
|
||||||
|
}
|
Loading…
Reference in new issue