fix problem Formualire

testTony
Tony Fages 1 year ago
parent f5f94a3f80
commit 409edd4199

@ -43,9 +43,9 @@ namespace API.Controllers
[HttpPost("/article")] [HttpPost("/article")]
public async Task<Article?> CreateArticle(long id, string title, string description, string author, string date, int lectureTime) public async Task<Article?> CreateArticle(Article article)
{ {
return await _articleService.CreateArticle(id, title, description, author, date, lectureTime); return await _articleService.CreateArticle(article);
} }
[HttpDelete("/article/{id}")] [HttpDelete("/article/{id}")]

@ -76,16 +76,15 @@ public class DbManagerArticle : IArticleService
} }
public async Task<Article?> CreateArticle(long id, string title, string description, string author, string date, int lectureTime) public async Task<Article?> CreateArticle(Article article)
{ {
var entity = new Entities.ArticleEntity() var entity = new Entities.ArticleEntity()
{ {
Id = id, Title = article.Title,
Title = title, Description = article.Description,
Description = description, Author = article.Author,
Author = author, DatePublished = article.DatePublished,
DatePublished = date, LectureTime = article.LectureTime,
LectureTime = lectureTime,
}; };
_context.ArticleSet.Add(entity); _context.ArticleSet.Add(entity);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();

@ -33,7 +33,7 @@ public class DbManagerFormulaire : IFormulaireService
formulaireList = _context.FormSet.OrderBy(f => f.DatePublication).Select(f => f.ToModel()).ToList(); formulaireList = _context.FormSet.OrderBy(f => f.DatePublication).Select(f => f.ToModel()).ToList();
break; break;
case FormOrderCriteria.ByPseudo: case FormOrderCriteria.ByPseudo:
formulaireList = _context.FormSet.OrderBy(f => f.Pseudo).Select(f => f.ToModel()).ToList(); formulaireList = _context.FormSet.OrderBy(f => f.UserEntityPseudo).Select(f => f.ToModel()).ToList();
break; break;
default: default:
formulaireList = _context.FormSet.Select(f => f.ToModel()).ToList(); formulaireList = _context.FormSet.Select(f => f.ToModel()).ToList();
@ -54,9 +54,10 @@ public class DbManagerFormulaire : IFormulaireService
var entity = new FormEntity() var entity = new FormEntity()
{ {
Id = formulaire.Id, Id = formulaire.Id,
Pseudo = formulaire.Pseudo, Link = formulaire.Lien,
Theme = formulaire.Theme, Theme = formulaire.Theme,
DatePublication = formulaire.Date DatePublication = formulaire.Date,
UserEntityPseudo = formulaire.UserPseudo
}; };
_context.FormSet.Add(entity); _context.FormSet.Add(entity);
@ -77,9 +78,10 @@ public class DbManagerFormulaire : IFormulaireService
{ {
var entity = _context.FormSet.FirstOrDefault(f => f.Id == id); var entity = _context.FormSet.FirstOrDefault(f => f.Id == id);
if (entity == null) return false; if (entity == null) return false;
entity.Pseudo = formulaire.Pseudo;
entity.Theme = formulaire.Theme; entity.Theme = formulaire.Theme;
entity.DatePublication = formulaire.Date; entity.DatePublication = formulaire.Date;
entity.Link = formulaire.Lien;
entity.UserEntityPseudo = formulaire.UserPseudo;
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return true; return true;
} }

@ -26,10 +26,10 @@ public static class Extensions
=> new User{ Pseudo = user.Pseudo, Mdp = user.Mdp, Prenom = user.Prenom, Nom = user.Nom, Mail = user.Mail, Role = user.Role}; => 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) public static FormEntity ToEntity(this Formulaire form)
=> new FormEntity{ Id = form.Id, Pseudo = form.Pseudo, Theme = form.Theme, Link = form.Lien}; => new FormEntity{ Id = form.Id, UserEntityPseudo = form.UserPseudo, Theme = form.Theme, Link = form.Lien};
public static Formulaire ToModel(this FormEntity form) public static Formulaire ToModel(this FormEntity form)
=> new Formulaire{ Id = form.Id, Pseudo = form.Pseudo, Theme = form.Theme, Lien = form.Link}; => new Formulaire{ Id = form.Id, UserPseudo = form.UserEntityPseudo, Theme = form.Theme, Lien = form.Link};
} }

@ -11,7 +11,7 @@ public static class FormulaireMapping
Theme = f.Theme, Theme = f.Theme,
Date = f.Date, Date = f.Date,
Lien = f.Lien, Lien = f.Lien,
Pseudo = f.Pseudo UserPseudo = f.UserPseudo
}; };
public static Formulaire ToModel(this FormulaireDTO f) => new() public static Formulaire ToModel(this FormulaireDTO f) => new()
@ -20,6 +20,6 @@ public static class FormulaireMapping
Theme = f.Theme, Theme = f.Theme,
Date = f.Date, Date = f.Date,
Lien = f.Lien, Lien = f.Lien,
Pseudo = f.Pseudo UserPseudo = f.UserPseudo
}; };
} }

@ -6,7 +6,7 @@ public class FormulaireDTO
public string Theme { get; set; } public string Theme { get; set; }
public string Date { get; set; } public string Date { get; set; }
public string Lien { get; set; } public string Lien { get; set; }
public string Pseudo { get; set; } public string UserPseudo { get; set; }
} }

@ -10,8 +10,7 @@ namespace API_Services
Task<Article?> GetArticleById(int id, int index, int count, ArticleOrderCriteria orderCriterium); Task<Article?> GetArticleById(int id, int index, int count, ArticleOrderCriteria orderCriterium);
Task<Article?> CreateArticle(long id, string title, string description, string author, string date, Task<Article?> CreateArticle(Article article);
int lectureTime);
Task<Article?> DeleteArticle(long id); Task<Article?> DeleteArticle(long id);

@ -41,12 +41,12 @@ public class LibraryContext : DbContext
modelBuilder.Entity<UserEntity>() modelBuilder.Entity<UserEntity>()
.HasMany(u => u.Forms) .HasMany(u => u.Forms)
.WithOne(f => f.User) .WithOne(f => f.User)
.HasForeignKey(f => f.UserEntityId); .HasForeignKey(f => f.UserEntityPseudo);
modelBuilder.Entity<FormEntity>() modelBuilder.Entity<FormEntity>()
.HasOne(f => f.User) .HasOne(f => f.User)
.WithMany(u => u.Forms) .WithMany(u => u.Forms)
.HasForeignKey(f => f.UserEntityId); .HasForeignKey(f => f.UserEntityPseudo);
modelBuilder.Entity<ArticleEntity>().HasData( modelBuilder.Entity<ArticleEntity>().HasData(
new ArticleEntity new ArticleEntity
@ -83,24 +83,24 @@ public class LibraryContext : DbContext
modelBuilder.Entity<UserEntity>().HasData( modelBuilder.Entity<UserEntity>().HasData(
new UserEntity new UserEntity
{ {
Id = 1, Nom = "Fages", Prenom = "Tony", Pseudo = "TonyF", Mail = "tony@gmail.com", Mdp = "1234", Role = "Admin" Nom = "Fages", Prenom = "Tony", Pseudo = "TonyF", Mail = "tony@gmail.com", Mdp = "1234", Role = "Admin"
}, },
new UserEntity new UserEntity
{ {
Id = 2, Nom = "Smith", Prenom = "Tom", Pseudo = "TomS", Mail = "tom@mail.com", Mdp = "1234", Nom = "Smith", Prenom = "Tom", Pseudo = "TomS", Mail = "tom@mail.com", Mdp = "1234",
Role = "User" Role = "User"
}, },
new UserEntity new UserEntity
{ {
Id = 3, Nom = "M&M's", Prenom = "Red", Pseudo = "RedM", Mail = "M&M#mail.com", Mdp = "1234", Role = "Modérator" Nom = "M&M's", Prenom = "Red", Pseudo = "RedM", Mail = "M&M#mail.com", Mdp = "1234", Role = "Modérator"
}, },
new UserEntity new UserEntity
{ {
Id = 4, Nom = "Cascarra", Prenom = "Cascarra", Pseudo = "Sha", Mail = "ShaCasca@gmail.com", Mdp = "1234", Role = "Admin" Nom = "Cascarra", Prenom = "Cascarra", Pseudo = "Sha", Mail = "ShaCasca@gmail.com", Mdp = "1234", Role = "Admin"
}, },
new UserEntity new UserEntity
{ {
Id = 5, Nom = "Sillard", Prenom = "Noa", Pseudo = "NoaSil", Mail = "", Mdp = "1234", Role = "Admin" Nom = "Sillard", Prenom = "Noa", Pseudo = "NoaSil", Mail = "", Mdp = "1234", Role = "Admin"
} }
); );
@ -108,27 +108,27 @@ public class LibraryContext : DbContext
new ArticleUserEntity new ArticleUserEntity
{ {
ArticleEntityId = 1, ArticleEntityId = 1,
UserEntityId = 1 UserEntityPseudo = "TonyF"
}, },
new ArticleUserEntity new ArticleUserEntity
{ {
ArticleEntityId = 2, ArticleEntityId = 2,
UserEntityId = 2 UserEntityPseudo = "NoaSil"
}, },
new ArticleUserEntity new ArticleUserEntity
{ {
ArticleEntityId = 3, ArticleEntityId = 3,
UserEntityId = 3 UserEntityPseudo = "Sha"
}, },
new ArticleUserEntity new ArticleUserEntity
{ {
ArticleEntityId = 3, ArticleEntityId = 3,
UserEntityId = 1 UserEntityPseudo = "RedM"
}, },
new ArticleUserEntity new ArticleUserEntity
{ {
ArticleEntityId = 2, ArticleEntityId = 2,
UserEntityId = 3 UserEntityPseudo = "TomS"
} }
); );
@ -136,26 +136,23 @@ public class LibraryContext : DbContext
new FormEntity new FormEntity
{ {
Id = 1, Id = 1,
Pseudo= "Form 1",
DatePublication = "Form 1 Description", DatePublication = "Form 1 Description",
Link = "hhtp://form1.com", Link = "hhtp://form1.com",
UserEntityId = 1 UserEntityPseudo = "Sha"
}, },
new FormEntity new FormEntity
{ {
Id = 2, Id = 2,
Pseudo= "Form 2",
DatePublication = "Form 2 Description", DatePublication = "Form 2 Description",
Link = "hhtp://form2.com", Link = "hhtp://form2.com",
UserEntityId = 2 UserEntityPseudo = "Sha"
}, },
new FormEntity new FormEntity
{ {
Id = 3, Id = 3,
Pseudo= "Form 3",
DatePublication = "Form 3 Description", DatePublication = "Form 3 Description",
Link = "hhtp://form3.com", Link = "hhtp://form3.com",
UserEntityId = 3 UserEntityPseudo = "Sha"
} }
); );
} }

@ -2,6 +2,6 @@ namespace Entities;
public class ArticleUserEntity public class ArticleUserEntity
{ {
public long UserEntityId { get; set; } public string UserEntityPseudo { get; set; }
public long ArticleEntityId { get; set; } public long ArticleEntityId { get; set; }
} }

@ -6,11 +6,8 @@ public class FormEntity
public string Theme { get; set; } = string.Empty; public string Theme { get; set; } = string.Empty;
public string DatePublication { get; set; } = string.Empty; public string DatePublication { get; set; } = string.Empty;
public string Link { get; set; } = string.Empty; public string Link { get; set; } = string.Empty;
public string Pseudo { get; set; } = string.Empty;
public long UserEntityId { get; set; }
public UserEntity User { get; set; } = null;
public string UserEntityPseudo{ get; set; }
public UserEntity User { get; set; } = null!;
} }

@ -1,8 +1,10 @@
using System.ComponentModel.DataAnnotations;
namespace Entities; namespace Entities;
public class UserEntity public class UserEntity
{ {
public long Id { get; set; } [Key]
public string Pseudo { get; set; } = string.Empty; public string Pseudo { get; set; } = string.Empty;
public string Mdp { get; set; } = string.Empty; public string Mdp { get; set; } = string.Empty;

@ -6,5 +6,5 @@ public class Formulaire
public string Theme { get; set; } public string Theme { get; set; }
public string Date { get; set; } public string Date { get; set; }
public string Lien { get; set; } public string Lien { get; set; }
public string Pseudo { get; set; } public string UserPseudo { get; set; }
} }

@ -45,16 +45,16 @@ public class StubbedContext : LibraryContext
modelBuilder.Entity<UserEntity>().HasData( modelBuilder.Entity<UserEntity>().HasData(
new UserEntity new UserEntity
{ {
Id = 1, Nom = "Fages", Prenom = "Tony", Pseudo = "TonyF", Mail = "tony@gmail.com", Mdp = "1234", Role = "Admin" Nom = "Fages", Prenom = "Tony", Pseudo = "TonyF", Mail = "tony@gmail.com", Mdp = "1234", Role = "Admin"
}, },
new UserEntity new UserEntity
{ {
Id = 2, Nom = "Smith", Prenom = "Tom", Pseudo = "TomS", Mail = "tom@mail.com", Mdp = "1234", Nom = "Smith", Prenom = "Tom", Pseudo = "TomS", Mail = "tom@mail.com", Mdp = "1234",
Role = "User" Role = "User"
}, },
new UserEntity new UserEntity
{ {
Id = 3, Nom = "M&M's", Prenom = "Red", Pseudo = "RedM", Mail = "M&M#mail.com", Mdp = "1234", Role = "Modérator" Nom = "M&M's", Prenom = "Red", Pseudo = "RedM", Mail = "M&M#mail.com", Mdp = "1234", Role = "Modérator"
} }
); );
@ -62,27 +62,27 @@ public class StubbedContext : LibraryContext
new ArticleUserEntity new ArticleUserEntity
{ {
ArticleEntityId = 1, ArticleEntityId = 1,
UserEntityId = 1 UserEntityPseudo = "Sha"
}, },
new ArticleUserEntity new ArticleUserEntity
{ {
ArticleEntityId = 2, ArticleEntityId = 2,
UserEntityId = 2 UserEntityPseudo = "Sha"
}, },
new ArticleUserEntity new ArticleUserEntity
{ {
ArticleEntityId = 3, ArticleEntityId = 3,
UserEntityId = 3 UserEntityPseudo = "Sha"
}, },
new ArticleUserEntity new ArticleUserEntity
{ {
ArticleEntityId = 3, ArticleEntityId = 3,
UserEntityId = 1 UserEntityPseudo = "Sha"
}, },
new ArticleUserEntity new ArticleUserEntity
{ {
ArticleEntityId = 2, ArticleEntityId = 2,
UserEntityId = 3 UserEntityPseudo = "Sha"
} }
); );
} }

@ -117,7 +117,7 @@ void listForms()
var forms = context.FormSet; var forms = context.FormSet;
foreach (var form in forms) foreach (var form in forms)
{ {
Console.WriteLine($"{form.Id} - {form.Link} - {form.DatePublication} - {form.Pseudo} - {form.Theme} - {form.UserEntityId}"); Console.WriteLine($"{form.Id} - {form.Link} - {form.DatePublication} - {form.Theme} - {form.UserEntityPseudo}");
} }
} }
} }
@ -132,8 +132,7 @@ void addForm()
Theme = "Covid", Theme = "Covid",
DatePublication = "16-02-2024", DatePublication = "16-02-2024",
Link = "https://www.covid.com", Link = "https://www.covid.com",
Pseudo = "Tony Fages", UserEntityPseudo = "Sha"
UserEntityId = 1
}; };
context.FormSet.Add(form); context.FormSet.Add(form);
context.SaveChanges(); context.SaveChanges();
@ -175,7 +174,7 @@ void listUser()
var users = context.UserSet; var users = context.UserSet;
foreach (var user in users) foreach (var user in users)
{ {
Console.WriteLine($"{user.Id} - {user.Pseudo} - {user.Nom} - {user.Prenom} - {user.Mail} - {user.Role}"); Console.WriteLine($" {user.Pseudo} - {user.Nom} - {user.Prenom} - {user.Mail} - {user.Role}");
} }
} }
} }
@ -186,7 +185,7 @@ void addUser()
{ {
var user = new UserEntity var user = new UserEntity
{ {
Id = 7, Nom = "Fages", Prenom = "Tony", Pseudo = "TonyF", Mail = "tony@gmail.com", Mdp = "1234", Role = "Admin" Nom = "Fages", Prenom = "Tony", Pseudo = "TonyF", Mail = "tony@gmail.com", Mdp = "1234", Role = "Admin"
}; };
context.UserSet.Add(user); context.UserSet.Add(user);
context.SaveChanges(); context.SaveChanges();
@ -198,7 +197,7 @@ void updateUser()
{ {
using (var context = new LibraryContext()) using (var context = new LibraryContext())
{ {
var user = context.UserSet.Where(u => u.Id.Equals(7)); var user = context.UserSet.Where(u => u.Pseudo.Equals("Sha"));
foreach (var users in user) foreach (var users in user)
{ {
@ -213,7 +212,7 @@ void deleteUser()
{ {
using (var context = new LibraryContext()) using (var context = new LibraryContext())
{ {
var user = context.UserSet.Where(u => u.Id.Equals(7)); var user = context.UserSet.Where(u => u.Pseudo.Equals("Sha"));
foreach (var users in user) foreach (var users in user)
{ {
@ -231,7 +230,7 @@ void listArticleUser()
var articleUsers = context.ArticleUserSet; var articleUsers = context.ArticleUserSet;
foreach (var articleUser in articleUsers) foreach (var articleUser in articleUsers)
{ {
Console.WriteLine($"{articleUser.ArticleEntityId} - {articleUser.UserEntityId}"); Console.WriteLine($"{articleUser.ArticleEntityId} - {articleUser.UserEntityPseudo}");
} }
} }
} }
@ -243,7 +242,7 @@ void addArticleUser()
var articleUser = new ArticleUserEntity var articleUser = new ArticleUserEntity
{ {
ArticleEntityId = 2, ArticleEntityId = 2,
UserEntityId = 1 UserEntityPseudo = "Sha"
}; };
context.ArticleUserSet.Add(articleUser); context.ArticleUserSet.Add(articleUser);
context.SaveChanges(); context.SaveChanges();
@ -255,8 +254,8 @@ void updateArticleUser()
{ {
using (var context = new LibraryContext()) using (var context = new LibraryContext())
{ {
var articleUser = context.ArticleUserSet.FirstOrDefault(au => au.UserEntityId.Equals(2)); var articleUser = context.ArticleUserSet.FirstOrDefault(au => au.UserEntityPseudo.Equals("Sha"));
if (articleUser != null) articleUser.UserEntityId = 3; if (articleUser != null) articleUser.UserEntityPseudo = "Sha";
context.SaveChanges(); context.SaveChanges();
} }
listArticleUser(); listArticleUser();
@ -266,7 +265,7 @@ void deleteArticleUser()
{ {
using (var context = new LibraryContext()) using (var context = new LibraryContext())
{ {
var articleUser = context.ArticleUserSet.Where(au => au.UserEntityId.Equals(1)).Where(u => u.ArticleEntityId.Equals(1)); var articleUser = context.ArticleUserSet.Where(au => au.UserEntityPseudo.Equals(1)).Where(u => u.ArticleEntityId.Equals(1));
foreach (var articleUsers in articleUser) foreach (var articleUsers in articleUser)
{ {
@ -281,10 +280,10 @@ void listFormUser()
{ {
using (var context = new LibraryContext()) using (var context = new LibraryContext())
{ {
var formUsers = context.FormSet; var formUsers = context.FormSet.Where(u => u.UserEntityPseudo.Equals(1));
foreach (var formUser in formUsers) foreach (var formUser in formUsers)
{ {
Console.WriteLine($"{formUser.UserEntityId}"); Console.WriteLine($"{formUser.UserEntityPseudo} - {formUser.Theme} - {formUser.Link}");
} }
} }
} }
@ -295,7 +294,7 @@ void addFormUser()
{ {
var formUser = new FormEntity var formUser = new FormEntity
{ {
UserEntityId = 1 UserEntityPseudo = "Sha",
}; };
context.FormSet.Add(formUser); context.FormSet.Add(formUser);
context.SaveChanges(); context.SaveChanges();

Loading…
Cancel
Save