Add COnsole Tests EF Form + User

testTony
Tony Fages 1 year ago
parent c70cd3cc0d
commit f5f94a3f80

@ -18,6 +18,28 @@ namespace API.Controllers
this._us = us;
}
[HttpGet("/users")]
public async Task<IActionResult> GetAll([FromQuery] int index = 0, [FromQuery] int count = 10, [FromQuery] UserOrderCriteria orderCriteria = UserOrderCriteria.None)
{
var result = (await _us.GetAll(index, count, orderCriteria)).Select(u => u.ToDTO());
if (result == null)
{
return NotFound();
}
return Ok(result);
}
[HttpGet("/user/{pseudo}")]
public async Task<IActionResult> GetByPseudo(string pseudo)
{
var result = (await _us.GetByPseudo(pseudo)).ToDTO();
if (result == null)
{
return NotFound();
}
return Ok(result);
}
[HttpPost("/user")]
public async Task<bool> Create(User user)
{
@ -37,27 +59,8 @@ namespace API.Controllers
return await _us.Delete(pseudo);
}
[HttpGet("/user/{pseudo}")]
public async Task<IActionResult> GetByPseudo(string pseudo)
{
var result = (await _us.GetByPseudo(pseudo)).ToDTO();
if (result == null)
{
return NotFound();
}
return Ok(result);
}
[HttpGet("/users")]
public async Task<IActionResult> GetAll([FromQuery] int index = 0, [FromQuery] int count = 10, [FromQuery] UserOrderCriteria orderCriteria = UserOrderCriteria.None)
{
var result = (await _us.GetAll(index, count, orderCriteria)).Select(u => u.ToDTO());
if (result == null)
{
return NotFound();
}
return Ok(result);
}
}
}

@ -38,8 +38,8 @@ app.MapControllers();
using var scoped = app.Services.CreateScope();
var libraryContext = scoped.ServiceProvider.GetService<LibraryContext>();
//libraryContext.Database.EnsureCreated();
libraryContext.Database.Migrate();
libraryContext.Database.EnsureCreated();
//libraryContext.Database.Migrate();
app.Run();

@ -19,6 +19,8 @@ public class LibraryContext : DbContext
public DbSet<UserEntity> UserSet { get; set; }
public DbSet<FormEntity> FormSet { get; set; }
public DbSet<ArticleUserEntity> ArticleUserSet { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
@ -91,6 +93,14 @@ public class LibraryContext : DbContext
new UserEntity
{
Id = 3, Nom = "M&M's", Prenom = "Red", Pseudo = "RedM", Mail = "M&M#mail.com", Mdp = "1234", Role = "Modérator"
},
new UserEntity
{
Id = 4, Nom = "Cascarra", Prenom = "Cascarra", Pseudo = "Sha", Mail = "ShaCasca@gmail.com", Mdp = "1234", Role = "Admin"
},
new UserEntity
{
Id = 5, Nom = "Sillard", Prenom = "Noa", Pseudo = "NoaSil", Mail = "", Mdp = "1234", Role = "Admin"
}
);

@ -1,16 +1,16 @@
// <auto-generated />
using DbContextLib;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using StubbedContextLib;
#nullable disable
namespace StubbedContextLib.Migrations
namespace DbContextLib.Migrations
{
[DbContext(typeof(StubbedContext))]
[Migration("20240307182411_mrg1")]
[DbContext(typeof(LibraryContext))]
[Migration("20240311132206_mrg1")]
partial class mrg1
{
/// <inheritdoc />
@ -90,7 +90,7 @@ namespace StubbedContextLib.Migrations
b.HasIndex("UserEntityId");
b.ToTable("ArticleUserEntity");
b.ToTable("ArticleUserSet");
b.HasData(
new
@ -150,6 +150,35 @@ namespace StubbedContextLib.Migrations
b.HasIndex("UserEntityId");
b.ToTable("FormSet");
b.HasData(
new
{
Id = 1L,
DatePublication = "Form 1 Description",
Link = "hhtp://form1.com",
Pseudo = "Form 1",
Theme = "",
UserEntityId = 1L
},
new
{
Id = 2L,
DatePublication = "Form 2 Description",
Link = "hhtp://form2.com",
Pseudo = "Form 2",
Theme = "",
UserEntityId = 2L
},
new
{
Id = 3L,
DatePublication = "Form 3 Description",
Link = "hhtp://form3.com",
Pseudo = "Form 3",
Theme = "",
UserEntityId = 3L
});
});
modelBuilder.Entity("Entities.UserEntity", b =>
@ -216,6 +245,26 @@ namespace StubbedContextLib.Migrations
Prenom = "Red",
Pseudo = "RedM",
Role = "Modérator"
},
new
{
Id = 4L,
Mail = "ShaCasca@gmail.com",
Mdp = "1234",
Nom = "Cascarra",
Prenom = "Cascarra",
Pseudo = "Sha",
Role = "Admin"
},
new
{
Id = 5L,
Mail = "",
Mdp = "1234",
Nom = "Sillard",
Prenom = "Noa",
Pseudo = "NoaSil",
Role = "Admin"
});
});

@ -4,7 +4,7 @@
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
namespace StubbedContextLib.Migrations
namespace DbContextLib.Migrations
{
/// <inheritdoc />
public partial class mrg1 : Migration
@ -48,7 +48,7 @@ namespace StubbedContextLib.Migrations
});
migrationBuilder.CreateTable(
name: "ArticleUserEntity",
name: "ArticleUserSet",
columns: table => new
{
UserEntityId = table.Column<long>(type: "INTEGER", nullable: false),
@ -56,15 +56,15 @@ namespace StubbedContextLib.Migrations
},
constraints: table =>
{
table.PrimaryKey("PK_ArticleUserEntity", x => new { x.ArticleEntityId, x.UserEntityId });
table.PrimaryKey("PK_ArticleUserSet", x => new { x.ArticleEntityId, x.UserEntityId });
table.ForeignKey(
name: "FK_ArticleUserEntity_ArticleSet_ArticleEntityId",
name: "FK_ArticleUserSet_ArticleSet_ArticleEntityId",
column: x => x.ArticleEntityId,
principalTable: "ArticleSet",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_ArticleUserEntity_UserSet_UserEntityId",
name: "FK_ArticleUserSet_UserSet_UserEntityId",
column: x => x.UserEntityId,
principalTable: "UserSet",
principalColumn: "Id",
@ -111,11 +111,13 @@ namespace StubbedContextLib.Migrations
{
{ 1L, "tony@gmail.com", "1234", "Fages", "Tony", "TonyF", "Admin" },
{ 2L, "tom@mail.com", "1234", "Smith", "Tom", "TomS", "User" },
{ 3L, "M&M#mail.com", "1234", "M&M's", "Red", "RedM", "Modérator" }
{ 3L, "M&M#mail.com", "1234", "M&M's", "Red", "RedM", "Modérator" },
{ 4L, "ShaCasca@gmail.com", "1234", "Cascarra", "Cascarra", "Sha", "Admin" },
{ 5L, "", "1234", "Sillard", "Noa", "NoaSil", "Admin" }
});
migrationBuilder.InsertData(
table: "ArticleUserEntity",
table: "ArticleUserSet",
columns: new[] { "ArticleEntityId", "UserEntityId" },
values: new object[,]
{
@ -126,9 +128,19 @@ namespace StubbedContextLib.Migrations
{ 3L, 3L }
});
migrationBuilder.InsertData(
table: "FormSet",
columns: new[] { "Id", "DatePublication", "Link", "Pseudo", "Theme", "UserEntityId" },
values: new object[,]
{
{ 1L, "Form 1 Description", "hhtp://form1.com", "Form 1", "", 1L },
{ 2L, "Form 2 Description", "hhtp://form2.com", "Form 2", "", 2L },
{ 3L, "Form 3 Description", "hhtp://form3.com", "Form 3", "", 3L }
});
migrationBuilder.CreateIndex(
name: "IX_ArticleUserEntity_UserEntityId",
table: "ArticleUserEntity",
name: "IX_ArticleUserSet_UserEntityId",
table: "ArticleUserSet",
column: "UserEntityId");
migrationBuilder.CreateIndex(
@ -141,7 +153,7 @@ namespace StubbedContextLib.Migrations
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "ArticleUserEntity");
name: "ArticleUserSet");
migrationBuilder.DropTable(
name: "FormSet");

@ -1,15 +1,15 @@
// <auto-generated />
using DbContextLib;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using StubbedContextLib;
#nullable disable
namespace StubbedContextLib.Migrations
namespace DbContextLib.Migrations
{
[DbContext(typeof(StubbedContext))]
partial class StubbedContextModelSnapshot : ModelSnapshot
[DbContext(typeof(LibraryContext))]
partial class LibraryContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
@ -87,7 +87,7 @@ namespace StubbedContextLib.Migrations
b.HasIndex("UserEntityId");
b.ToTable("ArticleUserEntity");
b.ToTable("ArticleUserSet");
b.HasData(
new
@ -147,6 +147,35 @@ namespace StubbedContextLib.Migrations
b.HasIndex("UserEntityId");
b.ToTable("FormSet");
b.HasData(
new
{
Id = 1L,
DatePublication = "Form 1 Description",
Link = "hhtp://form1.com",
Pseudo = "Form 1",
Theme = "",
UserEntityId = 1L
},
new
{
Id = 2L,
DatePublication = "Form 2 Description",
Link = "hhtp://form2.com",
Pseudo = "Form 2",
Theme = "",
UserEntityId = 2L
},
new
{
Id = 3L,
DatePublication = "Form 3 Description",
Link = "hhtp://form3.com",
Pseudo = "Form 3",
Theme = "",
UserEntityId = 3L
});
});
modelBuilder.Entity("Entities.UserEntity", b =>
@ -213,6 +242,26 @@ namespace StubbedContextLib.Migrations
Prenom = "Red",
Pseudo = "RedM",
Role = "Modérator"
},
new
{
Id = 4L,
Mail = "ShaCasca@gmail.com",
Mdp = "1234",
Nom = "Cascarra",
Prenom = "Cascarra",
Pseudo = "Sha",
Role = "Admin"
},
new
{
Id = 5L,
Mail = "",
Mdp = "1234",
Nom = "Sillard",
Prenom = "Noa",
Pseudo = "NoaSil",
Role = "Admin"
});
});

@ -1,26 +0,0 @@
using Entities;
using Model;
namespace StubbedContextLib;
public class StubTest
{
private List<Article> _article;
public List<Article> StubArticle()
{
_article = new List<Article>
{
new Article
{
Id = 1,
Title = "Test",
Description = "Test",
Author = "Test",
DatePublished = "Test",
LectureTime = 1
}
};
return _article;
}
}

@ -1,83 +0,0 @@
// See https://aka.ms/new-console-template for more information
using DbContextLib;
using Entities;
addArticle();
listArticle();
// Allows to list all the articles from the db
void listArticle()
{
using (var context = new LibraryContext())
{
var articles = context.ArticleSet;
foreach (var article in articles)
{
Console.WriteLine($"{article.Author} - {article.Title} - {article.Description} - {article.DatePublished} - {article.LectureTime}");
}
}
}
// Allows to list all the articles from the db by author
void listArticleByAuthor()
{
using (var context = new LibraryContext())
{
var articles = context.ArticleSet.Where(a => a.Author.Equals("Tony Fages"));
foreach (var article in articles)
{
Console.WriteLine($"{article.Author} - {article.Title} - {article.Description} - {article.DatePublished} - {article.LectureTime}");
}
}
}
// Allows to add an article to the db
void addArticle()
{
using (var context = new LibraryContext())
{
var article = new ArticleEntity
{
Title = "Louis is not sick anymore",
Description = "Louis is not sick anymore, he is now healthy and happy",
DatePublished = "16-02-2024",
LectureTime = 1,
Author = "Tony Fages"
};
context.ArticleSet.Add(article);
context.SaveChanges();
}
}
// Allows to modify an article from the db
void modifyArticle()
{
using (var context = new LibraryContext())
{
var article = context.ArticleSet.Where(a => a.Author.Equals("Tom Smith"));
foreach (var articles in article)
{
articles.Title = "Demain des l'aube";
context.SaveChanges();
}
}
}
// Allows to delete an article from the db
void deleteArticle()
{
using (var context = new LibraryContext())
{
var article = context.ArticleSet.Where(a => a.Author.Equals("M&M's Red"));
foreach (var articles in article)
{
context.ArticleSet.Remove(articles);
context.SaveChanges();
}
}
}

@ -0,0 +1,304 @@
// See https://aka.ms/new-console-template for more information
using DbContextLib;
using Entities;
//Article
//addArticle();
listArticle();
//modifyArticle();
//deleteArticle();
//listArticleByAuthor();
//Form
//addForm();
listForms();
//updateForm();
//deleteForm();
//User
//listUser();
//addUser();
//updateUser();
//deleteUser();
//ArticleUser
//listArticleUser();
//addArticleUser();
//updateArticleUser();
//deleteArticleUser();
//FormUser
listFormUser();
//addFormUser();
// Allows to list all the articles from the db
void listArticle()
{
using (var context = new LibraryContext())
{
var articles = context.ArticleSet;
foreach (var article in articles)
{
Console.WriteLine($"{article.Author} - {article.Title} - {article.Description} - {article.DatePublished} - {article.LectureTime}");
}
}
}
// Allows to list all the articles from the db by author
void listArticleByAuthor()
{
using (var context = new LibraryContext())
{
var articles = context.ArticleSet.Where(a => a.Author.Equals("Tony Fages"));
foreach (var article in articles)
{
Console.WriteLine($"{article.Author} - {article.Title} - {article.Description} - {article.DatePublished} - {article.LectureTime}");
}
}
}
// Allows to add an article to the db
void addArticle()
{
using (var context = new LibraryContext())
{
var article = new ArticleEntity
{
Title = "Louis is not sick anymore",
Description = "Louis is not sick anymore, he is now healthy and happy",
DatePublished = "16-02-2024",
LectureTime = 1,
Author = "Tony Fages"
};
context.ArticleSet.Add(article);
context.SaveChanges();
}
}
// Allows to modify an article from the db
void modifyArticle()
{
using (var context = new LibraryContext())
{
var article = context.ArticleSet.Where(a => a.Author.Equals("Tom Smith"));
foreach (var articles in article)
{
articles.Title = "Demain des l'aube";
context.SaveChanges();
}
}
}
// Allows to delete an article from the db
void deleteArticle()
{
using (var context = new LibraryContext())
{
var article = context.ArticleSet.Where(a => a.Author.Equals("M&M's Red"));
foreach (var articles in article)
{
context.ArticleSet.Remove(articles);
context.SaveChanges();
}
}
}
// Allow to get all forms
void listForms()
{
using (var context = new LibraryContext())
{
var forms = context.FormSet;
foreach (var form in forms)
{
Console.WriteLine($"{form.Id} - {form.Link} - {form.DatePublication} - {form.Pseudo} - {form.Theme} - {form.UserEntityId}");
}
}
}
void addForm()
{
using (var context = new LibraryContext())
{
var form = new FormEntity
{
Id = 5,
Theme = "Covid",
DatePublication = "16-02-2024",
Link = "https://www.covid.com",
Pseudo = "Tony Fages",
UserEntityId = 1
};
context.FormSet.Add(form);
context.SaveChanges();
}
}
void updateForm()
{
using (var context = new LibraryContext())
{
var form = context.FormSet.Where(f => f.Id.Equals(5));
foreach (var forms in form)
{
forms.Theme = "Demain des l'aube";
context.SaveChanges();
}
}
}
void deleteForm()
{
using (var context = new LibraryContext())
{
var form = context.FormSet.Where(f => f.Id.Equals(5));
foreach (var forms in form)
{
context.FormSet.Remove(forms);
context.SaveChanges();
}
}
}
void listUser()
{
using (var context = new LibraryContext())
{
var users = context.UserSet;
foreach (var user in users)
{
Console.WriteLine($"{user.Id} - {user.Pseudo} - {user.Nom} - {user.Prenom} - {user.Mail} - {user.Role}");
}
}
}
void addUser()
{
using (var context = new LibraryContext())
{
var user = new UserEntity
{
Id = 7, Nom = "Fages", Prenom = "Tony", Pseudo = "TonyF", Mail = "tony@gmail.com", Mdp = "1234", Role = "Admin"
};
context.UserSet.Add(user);
context.SaveChanges();
}
listUser();
}
void updateUser()
{
using (var context = new LibraryContext())
{
var user = context.UserSet.Where(u => u.Id.Equals(7));
foreach (var users in user)
{
users.Nom = "Thomas";
context.SaveChanges();
}
}
listUser();
}
void deleteUser()
{
using (var context = new LibraryContext())
{
var user = context.UserSet.Where(u => u.Id.Equals(7));
foreach (var users in user)
{
context.UserSet.Remove(users);
context.SaveChanges();
}
}
listUser();
}
void listArticleUser()
{
using (var context = new LibraryContext())
{
var articleUsers = context.ArticleUserSet;
foreach (var articleUser in articleUsers)
{
Console.WriteLine($"{articleUser.ArticleEntityId} - {articleUser.UserEntityId}");
}
}
}
void addArticleUser()
{
using (var context = new LibraryContext())
{
var articleUser = new ArticleUserEntity
{
ArticleEntityId = 2,
UserEntityId = 1
};
context.ArticleUserSet.Add(articleUser);
context.SaveChanges();
}
listArticleUser();
}
void updateArticleUser()
{
using (var context = new LibraryContext())
{
var articleUser = context.ArticleUserSet.FirstOrDefault(au => au.UserEntityId.Equals(2));
if (articleUser != null) articleUser.UserEntityId = 3;
context.SaveChanges();
}
listArticleUser();
}
void deleteArticleUser()
{
using (var context = new LibraryContext())
{
var articleUser = context.ArticleUserSet.Where(au => au.UserEntityId.Equals(1)).Where(u => u.ArticleEntityId.Equals(1));
foreach (var articleUsers in articleUser)
{
context.ArticleUserSet.Remove(articleUsers);
context.SaveChanges();
}
}
listArticleUser();
}
void listFormUser()
{
using (var context = new LibraryContext())
{
var formUsers = context.FormSet;
foreach (var formUser in formUsers)
{
Console.WriteLine($"{formUser.UserEntityId}");
}
}
}
void addFormUser()
{
using (var context = new LibraryContext())
{
var formUser = new FormEntity
{
UserEntityId = 1
};
context.FormSet.Add(formUser);
context.SaveChanges();
}
}
Loading…
Cancel
Save