COntrolleur + DBManager + Ca marche pas lol 🚧

testTony
Tony Fages 1 year ago
parent 36da6f99c1
commit 7afe33c430

@ -11,3 +11,5 @@
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# GitHub Copilot persisted chat sessions
/copilot/chatSessions

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\API_DbDataManager\API_DbDataManager.csproj" />
<ProjectReference Include="..\API_Services\API_Services.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,6 @@
@API_HostAddress = http://localhost:5052
GET {{API_HostAddress}}/weatherforecast/
Accept: application/json
###

@ -0,0 +1,64 @@
using API_Services;
using Microsoft.AspNetCore.Mvc;
using Model;
namespace API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ArticleController : ControllerBase
{
private readonly IArticleService _articleService;
public ArticleController(IArticleService articleService)
{
_articleService = articleService;
}
[HttpGet("/articles")]
public async Task<IActionResult> GetAllArticles()
{
var result = await _articleService.GetAllArticles();
if (result == null)
{
return NotFound();
}
return Ok(result);
}
[HttpGet("/article/{id}")]
public async Task<Article?> GetArticleById(int id)
{
var result = await _articleService.GetArticleById(id);
if (result == null)
{
return null;
}
return result;
}
[HttpPost("/article")]
public async Task<Article?> CreateArticle(long id, string title, string description, string author, string date, int lectureTime)
{
return await _articleService.CreateArticle(id, title, description, author, date, lectureTime);
}
[HttpDelete("/article/{id}")]
public async Task<Article?> DeleteArticle(long id)
{
return await _articleService.DeleteArticle(id);
}
[HttpPut("/article/{id}")]
public async Task<bool> UpdateArticle(long id, Article? a)
{
return await _articleService.UpdateArticle(id, a);
}
}
}

@ -0,0 +1,51 @@
using Microsoft.AspNetCore.Mvc;
using API_Services;
using Model;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class FormulaireController : ControllerBase
{
private readonly IFormulaireService _form;
public FormulaireController(IFormulaireService iform)
{
this._form = iform;
}
[HttpGet("/forms/{id}")]
public Task<IEnumerable<Formulaire?>> GetAllForm()
{
throw new NotImplementedException();
}
[HttpGet("{id}")]
public Task<Formulaire?> GetById(long id)
{
throw new NotImplementedException();
}
[HttpPost]
public Task<Formulaire?> CreateForm(Formulaire formulaire)
{
throw new NotImplementedException();
}
[HttpDelete("{id}")]
public Task<bool> DeleteForm(long id)
{
throw new NotImplementedException();
}
[HttpPut("{id}")]
public Task<bool> UpdateForm(long id, Formulaire formulaire)
{
throw new NotImplementedException();
}
}
}

@ -0,0 +1,51 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using API_Services;
using Model;
namespace API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
private readonly IUserService _us;
public UserController(IUserService us)
{
this._us = us;
}
[HttpPost("/user")]
public Task<bool> Create(User user)
{
throw new NotImplementedException();
}
[HttpPut("/user/{pseudo}")]
public Task<bool> Update(User user)
{
throw new NotImplementedException();
}
[HttpDelete("/user/{pseudo}")]
public Task<bool> Delete(string pseudo)
{
throw new NotImplementedException();
}
[HttpGet("/user/{pseudo}")]
public Task<User?> GetByPseudo(string pseudo)
{
throw new NotImplementedException();
}
[HttpGet("/users")]
public Task<IEnumerable<User?>> GetAll()
{
throw new NotImplementedException();
}
}
}

@ -0,0 +1,35 @@
using API_Services;
using DbContextLib;
using DbDataManager;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<LibraryContext>();
builder.Services.AddScoped<IArticleService, DbManagerArticle>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.Run();

@ -4,8 +4,8 @@
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:48061",
"sslPort": 44331
"applicationUrl": "http://localhost:16122",
"sslPort": 44361
}
},
"profiles": {
@ -14,7 +14,7 @@
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5139",
"applicationUrl": "http://localhost:5052",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
@ -24,7 +24,7 @@
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7143;http://localhost:5139",
"applicationUrl": "https://localhost:7159;http://localhost:5052",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}

@ -9,7 +9,7 @@
<ItemGroup>
<ProjectReference Include="..\API_Services\API_Services.csproj" />
<ProjectReference Include="..\Web_API\Web_API.csproj" />
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
</ItemGroup>
</Project>

@ -1,94 +0,0 @@
using API_Services;
using DbContextLib;
using Web_API.Mapper;
using Web_API.Model;
namespace DbDataManager;
public class DbDataManager : IArticleService, IFormulaireService, IUserService
{
private readonly LibraryContext _context;
private Mapper map = new Mapper();
public DbDataManager(LibraryContext context)
{
_context = context;
}
public Task<ArticleDTO> CreateArticle(ArticleDTO a)
{
throw new NotImplementedException();
}
public Task<ArticleDTO?> DeleteArticle(long id)
{
throw new NotImplementedException();
}
public Task<bool> UpdateArticle(long id, ArticleDTO a)
{
throw new NotImplementedException();
}
public Task<ArticleDTO> GetArticleById(int id)
{
var article = _context.ArticleSet.Find(id);
return Task.FromResult(map.ArtEntityToDTO(article));
}
public Task<List<ArticleDTO>> GetAllArticles()
{
var articles = _context.ArticleSet.ToList();
return Task.FromResult(map.ArtEntityToDTO(articles));
}
public Task<List<FormulaireDTO>> GetAllForm()
{
throw new NotImplementedException();
}
public Task<FormulaireDTO?> GetById(long id)
{
throw new NotImplementedException();
}
public Task<FormulaireDTO> CreateForm(Formulaire formulaire)
{
throw new NotImplementedException();
}
public Task<bool> DeleteForm(long id)
{
throw new NotImplementedException();
}
public Task<bool> UpdateForm(long id, Formulaire formulaire)
{
throw new NotImplementedException();
}
public Task<bool> Create(User user)
{
throw new NotImplementedException();
}
public Task<bool> Update(User user)
{
throw new NotImplementedException();
}
public Task<bool> Delete(string pseudo)
{
throw new NotImplementedException();
}
public Task<User> GetByPseudo(string pseudo)
{
throw new NotImplementedException();
}
public Task<List<UserDTO>> GetAll()
{
throw new NotImplementedException();
}
}

@ -0,0 +1,66 @@
using API_Services;
using DbContextLib;
using Entities;
using Model;
namespace DbDataManager;
public class DbManagerArticle : IArticleService
{
private readonly LibraryContext _context;
public DbManagerArticle(LibraryContext context)
{
_context = context;
}
public async Task<Article?> CreateArticle(long id, string title, string description, string author, string date, int lectureTime)
{
var entity = new ArticleEntity()
{
Id = id,
Title = title,
Description = description,
Author = author,
DatePublished = date,
LectureTime = lectureTime,
};
_context.ArticleSet.Add(entity);
await _context.SaveChangesAsync();
return entity.ToModel();
}
public async Task<Article?> DeleteArticle(long id)
{
var entity = _context.ArticleSet.FirstOrDefault(a => a.Id == id);
if (entity == null) return null;
_context.ArticleSet.Remove(entity);
await _context.SaveChangesAsync();
return entity.ToModel();
}
public async Task<bool> UpdateArticle(long id, Article? a)
{
var entity = _context.ArticleSet.FirstOrDefault(a => a.Id == id);
if (entity == null) return false;
entity.Title = a.Title;
entity.Description = a.Description;
entity.Author = a.Author;
entity.DatePublished = a.DatePublished;
entity.LectureTime = a.LectureTime;
await _context.SaveChangesAsync();
return true;
}
public Task<Article?> GetArticleById(int id)
{
var entity = _context.ArticleSet.FirstOrDefault(a => a.Id == id);
return Task.FromResult(entity.ToModel());
}
public async Task<IEnumerable<Article?>> GetAllArticles()
{
return await Task.FromResult(_context.ArticleSet.Select(a => a.ToModel()).AsEnumerable());
}
}

@ -0,0 +1,56 @@
using API_Services;
using DbContextLib;
using Entities;
using Model;
namespace DbDataManager;
public class DbManagerFormulaire(LibraryContext _context) : IFormulaireService
{
public async Task<IEnumerable<Formulaire?>> GetAllForm()
{
return await Task.FromResult(_context.FormSet.Select(f => f.ToModel()).AsEnumerable());
}
public async Task<Formulaire?> GetById(long id)
{
var entity = _context.FormSet.FirstOrDefault(f => f.Id == id);
if (entity != null) return await Task.FromResult(entity.ToModel());
return null;
}
public async Task<Formulaire?> CreateForm(Formulaire formulaire)
{
var entity = new FormEntity()
{
Id = formulaire.Id,
Pseudo = formulaire.Pseudo,
Theme = formulaire.Theme,
DatePublication = formulaire.Date
};
_context.FormSet.Add(entity);
await _context.SaveChangesAsync();
return entity.ToModel();
}
public async Task<bool> DeleteForm(long id)
{
var entity = _context.FormSet.FirstOrDefault(f => f.Id == id);
if (entity == null) return false;
_context.FormSet.Remove(entity);
await _context.SaveChangesAsync();
return true;
}
public async Task<bool> UpdateForm(long id, Formulaire formulaire)
{
var entity = _context.FormSet.FirstOrDefault(f => f.Id == id);
if (entity == null) return false;
entity.Pseudo = formulaire.Pseudo;
entity.Theme = formulaire.Theme;
entity.DatePublication = formulaire.Date;
await _context.SaveChangesAsync();
return true;
}
}

@ -0,0 +1,59 @@
using API_Services;
using DbContextLib;
using Entities;
using Model;
namespace DbDataManager;
public class DbManagerUser(LibraryContext _context): IUserService
{
public async Task<bool> Create(User user)
{
var entity = new UserEntity()
{
Pseudo = user.Pseudo,
Prenom = user.Prenom,
Nom = user.Nom,
Mdp = user.Mdp,
Mail = user.Mail,
Role = user.Role
};
_context.UserSet.Add(entity);
await _context.SaveChangesAsync();
return true;
}
public async Task<bool> Update(User user)
{
var entity = _context.UserSet.FirstOrDefault(u => u.Pseudo == user.Pseudo);
if (entity == null) return false;
entity.Mdp = user.Mdp;
entity.Mail = user.Mail;
entity.Role = user.Role;
entity.Prenom = user.Prenom;
entity.Nom = user.Nom;
await _context.SaveChangesAsync();
return true;
}
public async Task<bool> Delete(string pseudo)
{
var entity = _context.UserSet.FirstOrDefault(u => u.Pseudo == pseudo);
if (entity == null) return await Task.FromResult(false);
_context.UserSet.Remove(entity);
await _context.SaveChangesAsync();
return await Task.FromResult(true);
}
public async Task<User?> GetByPseudo(string pseudo)
{
var entity = _context.UserSet.FirstOrDefault(u => u.Pseudo == pseudo);
return await Task.FromResult(entity.ToModel());
}
public async Task<IEnumerable<User?>> GetAll()
{
return await Task.FromResult(_context.UserSet.Select(u => u.ToModel()).AsEnumerable());
}
}

@ -0,0 +1,35 @@
using Entities;
using Model;
namespace DbDataManager;
public static class Extensions
{
public static ArticleEntity ToEntity(this Article article)
=> new ArticleEntity
{
Id = article.Id, Author = article.Author, Description = article.Description, Title = article.Title,
DatePublished = article.DatePublished, LectureTime = article.LectureTime
};
public static Article ToModel(this ArticleEntity article)
=> new Article
{
Id = article.Id, Author = article.Author, Description = article.Description, Title = article.Title,
DatePublished = article.DatePublished, LectureTime = article.LectureTime
};
public static UserEntity ToEntity(this User user)
=> new UserEntity{ Pseudo = user.Pseudo, Mdp = user.Mdp, Prenom = user.Prenom, Nom = user.Nom, Mail = user.Mail, Role = user.Role};
public static User ToModel(this UserEntity user)
=> new User{ Pseudo = user.Pseudo, Mdp = user.Mdp, Prenom = user.Prenom, Nom = user.Nom, Mail = user.Mail, Role = user.Role};
public static FormEntity ToEntity(this Formulaire form)
=> new FormEntity{ Id = form.Id, Pseudo = form.Pseudo, Theme = form.Theme, Link = form.Lien};
public static Formulaire ToModel(this FormEntity form)
=> new Formulaire{ Id = form.Id, Pseudo = form.Pseudo, Theme = form.Theme, Lien = form.Link};
}

@ -4,7 +4,7 @@ public class FormulaireDTO
{
public long Id;
public string Theme { get; set; }
public DateTime Date { get; set; }
public string Date { get; set; }
public string Lien { get; set; }
public string Pseudo { get; set; }

@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Web_API\Web_API.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
</Project>

@ -1,19 +1,19 @@
using Entities;
using Web_API.Model;
using Model;
namespace API_Services
{
public interface IArticleService
{
Task<ArticleDTO> CreateArticle(ArticleDTO a);
Task<Article?> CreateArticle(long id, string title, string description, string author, string date,
int lectureTime);
Task<ArticleDTO?> DeleteArticle(long id);
Task<Article?> DeleteArticle(long id);
Task<bool> UpdateArticle(long id, ArticleDTO a);
Task<bool> UpdateArticle(long id, Article? a);
Task<ArticleDTO> GetArticleById(int id);
Task<Article?> GetArticleById(int id);
Task<List<ArticleDTO>> GetAllArticles();
Task<IEnumerable<Article?>> GetAllArticles();
}
}

@ -1,16 +1,16 @@
using Web_API.Model;
using Model;
namespace API_Services;
public interface IFormulaireService
{
Task<List<FormulaireDTO>> GetAllForm();
Task<IEnumerable<Formulaire?>> GetAllForm();
Task<FormulaireDTO?> GetById(long id);
Task<Formulaire?> GetById(long id);
Task<FormulaireDTO> CreateForm(Formulaire formulaire);
Task<Formulaire?> CreateForm(Formulaire formulaire);
Task<bool> DeleteForm(long id);

@ -1,4 +1,4 @@
using Web_API.Model;
using Model;
namespace API_Services
{
@ -10,9 +10,9 @@ namespace API_Services
Task<bool> Delete(string pseudo);
Task<User> GetByPseudo(string pseudo);
Task<User?> GetByPseudo(string pseudo);
Task<List<UserDTO>> GetAll();
Task<IEnumerable<User?>> GetAll();

@ -13,8 +13,11 @@ public class LibraryContext : DbContext
: base(options)
{ }
public DbSet<ArticleEntity> ArticleSet { get; set; }
public DbSet<UserEntity> UserSet { get; set; }
public DbSet<FormEntity> FormSet { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{

@ -4,7 +4,7 @@ public class Formulaire
{
public long Id;
public string Theme { get; set; }
public DateTime Date { get; set; }
public string Date { get; set; }
public string Lien { get; set; }
public string Pseudo { get; set; }
}

@ -8,8 +8,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbContextLib", "DbContextLi
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubbedContextLib", "StubbedContextLib\StubbedContextLib.csproj", "{F1B4BCE5-8DE7-4EFB-8BC1-D7E04EA75302}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Web_API", "Web_API\Web_API.csproj", "{852E0658-1A97-482A-84F3-0EF08E34D7B1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API_DbDataManager", "API_DbDataManager\API_DbDataManager.csproj", "{FBA0CF18-7488-4088-A7C5-5D2071D19CCB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API_Services", "API_Services\API_Services.csproj", "{4FB7D286-B583-44BC-BB79-4AE3058AFEDE}"
@ -20,6 +18,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API_Mapping", "API_Mapping\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Model", "Model\Model.csproj", "{96FDB5DE-6707-4856-94CD-EFAF0C7CEB4B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API", "API\API.csproj", "{6AACD337-70A0-429B-979C-1B1E004BF2E0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -42,10 +42,6 @@ Global
{F1B4BCE5-8DE7-4EFB-8BC1-D7E04EA75302}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F1B4BCE5-8DE7-4EFB-8BC1-D7E04EA75302}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F1B4BCE5-8DE7-4EFB-8BC1-D7E04EA75302}.Release|Any CPU.Build.0 = Release|Any CPU
{852E0658-1A97-482A-84F3-0EF08E34D7B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{852E0658-1A97-482A-84F3-0EF08E34D7B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{852E0658-1A97-482A-84F3-0EF08E34D7B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{852E0658-1A97-482A-84F3-0EF08E34D7B1}.Release|Any CPU.Build.0 = Release|Any CPU
{FBA0CF18-7488-4088-A7C5-5D2071D19CCB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FBA0CF18-7488-4088-A7C5-5D2071D19CCB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FBA0CF18-7488-4088-A7C5-5D2071D19CCB}.Release|Any CPU.ActiveCfg = Release|Any CPU
@ -66,5 +62,9 @@ Global
{96FDB5DE-6707-4856-94CD-EFAF0C7CEB4B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{96FDB5DE-6707-4856-94CD-EFAF0C7CEB4B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{96FDB5DE-6707-4856-94CD-EFAF0C7CEB4B}.Release|Any CPU.Build.0 = Release|Any CPU
{6AACD337-70A0-429B-979C-1B1E004BF2E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6AACD337-70A0-429B-979C-1B1E004BF2E0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6AACD337-70A0-429B-979C-1B1E004BF2E0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6AACD337-70A0-429B-979C-1B1E004BF2E0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

@ -1,68 +0,0 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Web_API.Model;
using API_Services;
namespace Web_API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ArticleController : ControllerBase
{
private readonly IArticleService _as;
private Mapper.Mapper map = new Mapper.Mapper();
public ArticleController(IArticleService articleService)
{
this._as = articleService;
}
[HttpGet("Articles")]
public async Task<IActionResult> GetAllArticle()
{
return Ok(await _as.GetAllArticles());
}
[HttpGet("{id}")]
public async Task<IActionResult> GetArticle(int id)
{
var article = await _as.GetById(id);
if (article == null)
{
return NotFound();
}
return Ok(article);
}
[HttpPost]
public async Task<ActionResult<Article>> PostArticle(ArticleDTO article)
{
var newArticle = await _as.Create(article);
if (newArticle == null) return BadRequest();
var newArticleEnt = map.ArtDTOToEntity(article);
return CreatedAtAction(nameof(GetArticle), new { id = newArticle.Id}, newArticleEnt);
}
[HttpPut("{id}")]
public async Task<ActionResult<Article>> PutArticle(long id , [FromBody]ArticleDTO article)
{
var check = await _as.Update(id,article);
if (!check) return NotFound();
var articleEnt = map.ArtDTOToEntity(article);
return articleEnt;
}
[HttpDelete("{id}")]
public async Task<ActionResult<Article>> DeleteArticle(long id)
{
var articleDeleted = await _as.Delete(id);
if (articleDeleted == null)return NotFound();
articleDeleted = map.ArtEntityToDTO(articleDeleted);
return Ok(articleDeleted);
}
}
}

@ -1,27 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Web_API.Model;
using API_Services;
namespace Web_API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class FormulaireController : ControllerBase
{
private readonly IFormulaireService _form;
private Mapper.Mapper map = new Mapper.Mapper();
public FormulaireController(IFormulaireService iform)
{
this._form = iform;
}
[HttpGet]
public async Task<List<Formulaire>> GetAllForm()
{
var AllForms = await _form.GetAllForm();
return AllForms;
}
}
}

@ -1,76 +0,0 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Web_API.Model;
using API_Services;
namespace Web_API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
private readonly ILogger<User>? logger;
private readonly IUserService _us;
public UserController(IUserService us)
{
this._us = us;
}
[HttpGet]
public async Task<IActionResult> GetAll()
{
return Ok(await _us.GetAll());
}
// GET : users/id
[HttpGet("{pseudo}", Name = "GetUserByPseudo")]
public async Task<IActionResult> GetUser(string pseudo)
{
var user = _us.GetByPseudo(pseudo);
if (user == null)
{
return NotFound();
}
return Ok(user);
}
[HttpPost]
public async Task<ActionResult<User>> PostUser(User user)
{
var check = await _us.Create(user);
if (!check)
{
return BadRequest();
}
return CreatedAtAction(nameof(GetUser), new { Pseudo = user.Pseudo}, user);
}
[HttpDelete]
public async Task<IActionResult> DeleteUser(string pseudo)
{
var check = await _us.Delete(pseudo);
if (!check)
{
return NotFound();
}
return Ok(pseudo);
}
[HttpPut]
public async Task<IActionResult> UpdateUser(User user)
{
var check = await _us.Update(user);
if (!check)
{
return NotFound();
}
return Ok(user);
}
}
}

@ -1,82 +0,0 @@
using Entities;
using Web_API.Model;
namespace Web_API.Mapper;
public class Mapper
{
public ArticleDTO ArtEntityToDTO(ArticleEntity a)
{
return new ArticleDTO
{
Id = a.Id,
Author = a.Author,
Title = a.Title,
Description = a.Description,
LectureTime = a.LectureTime,
DatePublished = a.DatePublished
};
}
public ArticleEntity ArtDTOToEntity(ArticleDTO a)
{
return new ArticleEntity()
{
Id = a.Id,
Author = a.Author,
Title = a.Title,
Description = a.Description,
LectureTime = a.LectureTime,
DatePublished = a.DatePublished
};
}
public FormulaireDTO FormEntityToDTO(FormEntity f)
{
return new FormulaireDTO
{
Theme = f.Theme,
Date = f.DatePublication,
Lien = f.Lien,
Pseudo = f.Pseudo
};
}
public Formulaire FormDTOToEntity(FormulaireDTO f)
{
return new Formulaire
{
Theme = f.Theme,
Date = f.Date,
Lien = f.Lien,
Pseudo = f.Pseudo
};
}
public UserDTO UserEntityToDTO(User u)
{
return new UserDTO
{
Pseudo = u.Pseudo,
Mail = u.Mail,
Prenom = u.Prenom,
Nom = u.Nom,
Role = u.Role,
Mdp = u.Mdp
};
}
public User UserDTOToEntity(UserDTO u)
{
return new User
{
Pseudo = u.Pseudo,
Mail = u.Mail,
Prenom = u.Prenom,
Nom = u.Nom,
Role = u.Role,
Mdp = u.Mdp
};
}
}

@ -1,44 +0,0 @@
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();
app.Run();
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}

@ -1,21 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\API_Model\API_Model.csproj" />
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
</ItemGroup>
</Project>

@ -1,6 +0,0 @@
@Web_API_HostAddress = http://localhost:5139
GET {{Web_API_HostAddress}}/weatherforecast/
Accept: application/json
###
Loading…
Cancel
Save