Suppression des constructeurs pour user et notepad

pull/40/head
Clement CHIEU 1 year ago
parent 99471a3705
commit c4dd76334d

@ -7,32 +7,10 @@ public class NotepadEntity
{
public int Id { get; set; }
[ForeignKey(nameof(User))]
public int UserId { get; set; }
[ForeignKey(nameof(User))] public int UserId { get; set; }
public UserEntity User { get; set; }
[ForeignKey(nameof(Inquiry))]
public int InquiryId { get; set; }
[ForeignKey(nameof(Inquiry))] public int InquiryId { get; set; }
public InquiryEntity Inquiry { get; set; }
public string Notes { get; set; }
public NotepadEntity() { }
public NotepadEntity(int id, int userId, UserEntity user, int inquiryId, InquiryEntity inquiry, string notes)
{
Id = id;
UserId = userId;
User = user;
InquiryId = inquiryId;
Inquiry = inquiry;
Notes = notes;
}
public NotepadEntity(int userId, UserEntity user, int inquiryId, InquiryEntity inquiry, string notes)
{
UserId = userId;
User = user;
InquiryId = inquiryId;
Inquiry = inquiry;
Notes = notes;
}
}

@ -10,30 +10,4 @@ public class UserEntity
public string Password { get; set; }
public string Email { get; set; }
public bool IsAdmin { get; set; }
public UserEntity()
{
}
public UserEntity(int id)
{
Id = id;
}
public UserEntity(int id, string username, string password, string email, bool isAdmin)
{
Id = id;
Username = username;
Password = password;
Email = email;
IsAdmin = isAdmin;
}
public UserEntity(string username, string password, string email, bool isAdmin)
{
Username = username;
Password = password;
Email = email;
IsAdmin = isAdmin;
}
}

@ -6,7 +6,7 @@ namespace Shared.Mapper;
public static class NotepadMapper
{
public static Notepad FromDTOToModel(this NotepadDTO dto)
public static Notepad FromDtoToModel(this NotepadDTO dto)
{
return new Notepad(dto.Id, dto.UserId, dto.InquiryId, dto.Notes);
}
@ -16,33 +16,49 @@ public static class NotepadMapper
return new Notepad(ent.Id, ent.UserId, ent.InquiryId, ent.Notes);
}
public static NotepadDTO FromModelToDTO(this Notepad not)
public static NotepadDTO FromModelToDto(this Notepad not)
{
return new NotepadDTO(not.Id, not.UserId, not.InquiryId, not.Notes);
}
public static NotepadDTO FromEntityToDTO(this NotepadEntity ent)
public static NotepadDTO FromEntityToDto(this NotepadEntity ent)
{
return new NotepadDTO(ent.Id, ent.UserId, ent.InquiryId, ent.Notes);
}
public static NotepadEntity FromDTOToEntity(this NotepadDTO dto)
public static NotepadEntity FromDtoToEntity(this NotepadDTO dto)
{
return new NotepadEntity(dto.Id, new UserEntity(dto.UserId), dto.UserId,
new InquiryEntity
return new NotepadEntity
{
Id = dto.Id,
User = new UserEntity
{
Id = dto.UserId
},
UserId = dto.UserId,
Inquiry = new InquiryEntity
{
Id = dto.InquiryId
},
dto.Notes);
Notes = dto.Notes
};
}
public static NotepadEntity FromModelToEntity(this Notepad not)
{
return new NotepadEntity(not.Id, new UserEntity(not.UserId), not.UserId,
new InquiryEntity
return new NotepadEntity
{
Id = not.Id,
User = new UserEntity
{
Id = not.UserId
},
UserId = not.UserId,
Inquiry = new InquiryEntity
{
Id = not.InquiryId
},
not.Notes);
Notes = not.Notes
};
}
}

@ -6,7 +6,7 @@ namespace Shared.Mapper;
public static class SuccessMapper
{
public static Success FromDTOToModel(this SuccessDTO dto)
public static Success FromDtoToModel(this SuccessDTO dto)
{
return new Success(dto.UserId, dto.InquiryId, dto.IsFinished);
}
@ -16,19 +16,23 @@ public static class SuccessMapper
return new Success(ent.UserId, ent.InquiryId, ent.IsFinished);
}
public static SuccessDTO FromModelToDTO(this Success suc)
public static SuccessDTO FromModelToDto(this Success suc)
{
return new SuccessDTO(suc.UserId, suc.InquiryId, suc.IsFinished);
}
public static SuccessDTO FromEntityToDTO(this SuccessEntity ent)
public static SuccessDTO FromEntityToDto(this SuccessEntity ent)
{
return new SuccessDTO(ent.UserId, ent.InquiryId, ent.IsFinished);
}
public static SuccessEntity FromDTOToEntity(this SuccessDTO dto)
public static SuccessEntity FromDtoToEntity(this SuccessDTO dto)
{
return new SuccessEntity(dto.UserId, new UserEntity(dto.UserId), dto.InquiryId,
return new SuccessEntity(dto.UserId, new UserEntity
{
Id = dto.UserId
},
dto.InquiryId,
new InquiryEntity
{
Id = dto.InquiryId
@ -38,7 +42,11 @@ public static class SuccessMapper
public static SuccessEntity FromModelToEntity(this Success suc)
{
return new SuccessEntity(suc.UserId, new UserEntity(suc.UserId), suc.InquiryId,
return new SuccessEntity(suc.UserId, new UserEntity
{
Id = suc.UserId
},
suc.InquiryId,
new InquiryEntity
{
Id = suc.InquiryId

@ -6,14 +6,21 @@ namespace Shared.Mapper;
public static class UserMapper
{
public static User FromDTOToModel(this UserDTO dto)
public static User FromDtoToModel(this UserDTO dto)
{
return new User(dto.Id, dto.Username, dto.Password, dto.Email, dto.IsAdmin);
}
public static UserEntity FromDTOToEntity(this UserDTO dto)
public static UserEntity FromDtoToEntity(this UserDTO dto)
{
return new UserEntity(dto.Id, dto.Username, dto.Password, dto.Email, dto.IsAdmin);
return new UserEntity
{
Id = dto.Id,
Username = dto.Username,
Password = dto.Password,
Email = dto.Email,
IsAdmin = dto.IsAdmin
};
}
public static User FromEntityToModel(this UserEntity entity)
@ -21,18 +28,25 @@ public static class UserMapper
return new User(entity.Id, entity.Username, entity.Password, entity.Email, entity.IsAdmin);
}
public static UserDTO FromEntityToDTO(this UserEntity entity)
public static UserDTO FromEntityToDto(this UserEntity entity)
{
return new UserDTO(entity.Id, entity.Username, entity.Password, entity.Email, entity.IsAdmin);
}
public static UserDTO FromModelToDTO(this User user)
public static UserDTO FromModelToDto(this User user)
{
return new UserDTO(user.Id, user.Username, user.Password, user.Email, user.IsAdmin);
}
public static UserEntity FromModelToEntity(this User user)
{
return new UserEntity(user.Id, user.Username, user.Password, user.Email, user.IsAdmin);
return new UserEntity
{
Id = user.Id,
Username = user.Username,
Password = user.Password,
Email = user.Email,
IsAdmin = user.IsAdmin
};
}
}

@ -35,7 +35,7 @@ public class SuccessDataService : ISuccessDataService
break;
}
var successes = query.ToList();
return successes.Select(s => s.FromEntityToDTO());
return successes.Select(s => s.FromEntityToDto());
}
public SuccessDTO GetSuccessByUserId(int id)
@ -46,7 +46,7 @@ public class SuccessDataService : ISuccessDataService
throw new ArgumentException("Impossible de trouver le succès", nameof(id));
}
return userEntity.FromEntityToDTO();
return userEntity.FromEntityToDto();
}
public SuccessDTO GetSuccessByInquiryId(int id)
@ -57,7 +57,7 @@ public class SuccessDataService : ISuccessDataService
throw new ArgumentException("Impossible de trouver le succès", nameof(id));
}
return userEntity.FromEntityToDTO();
return userEntity.FromEntityToDto();
}
public bool DeleteSuccess(int id)
@ -85,7 +85,7 @@ public class SuccessDataService : ISuccessDataService
updatingSuccess.InquiryId = success.InquiryId;
updatingSuccess.IsFinished = success.IsFinished;
DbContext.SaveChangesAsync();
return updatingSuccess.FromEntityToDTO();
return updatingSuccess.FromEntityToDto();
}
public SuccessDTO CreateSuccess(int userId, int inquiryId, bool isFinished)
@ -96,7 +96,7 @@ public class SuccessDataService : ISuccessDataService
InquiryId = inquiryId,
IsFinished = isFinished,
};
DbContext.Successes.Add(newSuccessEntity.FromDTOToEntity());
DbContext.Successes.Add(newSuccessEntity.FromDtoToEntity());
DbContext.SaveChangesAsync();
return newSuccessEntity;
}

@ -30,7 +30,7 @@ namespace Shared
throw new ArgumentException("Impossible de trouver l'utilisateur", nameof(id));
}
return userEntity.FromEntityToDTO();
return userEntity.FromEntityToDto();
}
@ -42,7 +42,7 @@ namespace Shared
throw new ArgumentException("Impossible de trouver l'utilisateur", nameof(username));
}
return userEntity.FromEntityToDTO();
return userEntity.FromEntityToDto();
}
@ -69,7 +69,7 @@ namespace Shared
break;
}
var users = query.ToList();
return users.Select(s => s.FromEntityToDTO());
return users.Select(s => s.FromEntityToDto());
}
public bool DeleteUser(int id)
@ -99,7 +99,7 @@ namespace Shared
updatingUser.Email = user.Email;
updatingUser.IsAdmin = user.IsAdmin;
DbContext.SaveChangesAsync();
return updatingUser.FromEntityToDTO();
return updatingUser.FromEntityToDto();
}
@ -112,7 +112,7 @@ namespace Shared
Email = email,
IsAdmin = isAdmin
};
DbContext.Users.Add(newUserEntity.FromDTOToEntity());
DbContext.Users.Add(newUserEntity.FromDtoToEntity());
DbContext.SaveChangesAsync();
return newUserEntity;

@ -17,36 +17,71 @@ public class StubbedContext : UserDbContext
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<UserEntity>().HasData(
new UserEntity(1, "johnny", Convert.ToBase64String(KeyDerivation.Pbkdf2(
new UserEntity
{
Id = 1,
Username = "johnny",
Password = Convert.ToBase64String(KeyDerivation.Pbkdf2(
password: "motdepasse",
salt: RandomNumberGenerator.GetBytes(128 / 8),
prf: KeyDerivationPrf.HMACSHA256,
iterationCount: 100000,
numBytesRequested: 256 / 8)), "Johnny.RATTON@etu.uca.fr", true),
new UserEntity(2, "maxime", Convert.ToBase64String(KeyDerivation.Pbkdf2(
numBytesRequested: 256 / 8)),
Email = "Johnny.RATTON@etu.uca.fr",
IsAdmin = true
},
new UserEntity
{
Id = 2,
Username = "maxime",
Password = Convert.ToBase64String(KeyDerivation.Pbkdf2(
password: "motdepasse",
salt: RandomNumberGenerator.GetBytes(128 / 8),
prf: KeyDerivationPrf.HMACSHA256,
iterationCount: 100000,
numBytesRequested: 256 / 8)), "Maxime.SAPOUNTZIS@etu.uca.fr", true),
new UserEntity(3, "clement", Convert.ToBase64String(KeyDerivation.Pbkdf2(
numBytesRequested: 256 / 8)),
Email = "Maxime.SAPOUNTZIS@etu.uca.fr",
IsAdmin = true
},
new UserEntity
{
Id = 3,
Username = "clement",
Password = Convert.ToBase64String(KeyDerivation.Pbkdf2(
password: "motdepasse",
salt: RandomNumberGenerator.GetBytes(128 / 8),
prf: KeyDerivationPrf.HMACSHA256,
iterationCount: 100000,
numBytesRequested: 256 / 8)), "Clement.CHIEU@etu.uca.fr", true),
new UserEntity(4, "erwan", Convert.ToBase64String(KeyDerivation.Pbkdf2(
numBytesRequested: 256 / 8)),
Email = "Clement.CHIEU@etu.uca.fr",
IsAdmin = true
},
new UserEntity
{
Id = 4,
Username = "erwan",
Password = Convert.ToBase64String(KeyDerivation.Pbkdf2(
password: "motdepasse",
salt: RandomNumberGenerator.GetBytes(128 / 8),
prf: KeyDerivationPrf.HMACSHA256,
iterationCount: 100000,
numBytesRequested: 256 / 8)), "Erwan.MENAGER@etu.uca.fr", true),
new UserEntity(5, "victor", Convert.ToBase64String(KeyDerivation.Pbkdf2(
numBytesRequested: 256 / 8)),
Email = "Erwan.MENAGER@etu.uca.fr",
IsAdmin = true
},
new UserEntity
{
Id = 5,
Username = "victor",
Password = Convert.ToBase64String(KeyDerivation.Pbkdf2(
password: "motdepasse",
salt: RandomNumberGenerator.GetBytes(128 / 8),
prf: KeyDerivationPrf.HMACSHA256,
iterationCount: 100000,
numBytesRequested: 256 / 8)), "Victor.GABORIT@etu.uca.fr", true));
numBytesRequested: 256 / 8)),
Email = "Victor.GABORIT@etu.uca.fr",
IsAdmin = true
});
modelBuilder.Entity<InquiryTableEntity>().HasData(
new InquiryTableEntity

@ -46,12 +46,19 @@ using (var db = new StubbedContext(options))
// Ajout d'un utilisateur
Console.WriteLine("\nAjout du nouvel utilisateur");
var newUser = new UserEntity("le nouveau du groupe", Convert.ToBase64String(KeyDerivation.Pbkdf2(
var newUser = new UserEntity
{
Username = "le nouveau du groupe",
Password = Convert.ToBase64String(KeyDerivation.Pbkdf2(
password: "motdepasse",
salt: RandomNumberGenerator.GetBytes(128 / 8),
prf: KeyDerivationPrf.HMACSHA256,
iterationCount: 100000,
numBytesRequested: 256 / 8)), "Efff.fffff@etu.uca.fr", true);
numBytesRequested: 256 / 8)),
Email = "Efff.fffff@etu.uca.fr",
IsAdmin = true
}
;
if (!users.Any(u => u.Username == newUser.Username))
{
users.Add(newUser);

@ -1,15 +1,15 @@
using Entities;
namespace TestEF;
namespace TestEF.EntitiesTests;
public class TestNotepadEntity
{
private const int _id = 42;
private const int _userId = 42;
private static UserEntity _userEntity = new UserEntity();
private const int _inquiryId = 42;
private static InquiryEntity _inquiryEntity = new InquiryEntity();
private const string _notes = "This is some notes example";
private const int Id = 42;
private const int UserId = 42;
private static readonly UserEntity UserEntity = new();
private const int InquiryId = 42;
private static readonly InquiryEntity InquiryEntity = new();
private const string Notes = "This is some notes example";
[Fact]
public void TestDefaultConstructor()
@ -26,24 +26,39 @@ public class TestNotepadEntity
[Fact]
public void TestConstructorWithoutId()
{
NotepadEntity notepad = new NotepadEntity(_userId,_userEntity,_inquiryId,_inquiryEntity,_notes);
NotepadEntity notepad = new NotepadEntity
{
UserId = UserId,
User = UserEntity,
InquiryId = InquiryId,
Inquiry = InquiryEntity,
Notes = Notes
};
Assert.Equal(0, notepad.Id);
Assert.Equal(_userId,notepad.UserId);
Assert.Equal(_userEntity,notepad.User);
Assert.Equal(_inquiryId,notepad.InquiryId);
Assert.Equal(_inquiryEntity,notepad.Inquiry);
Assert.Equal(_notes,notepad.Notes);
Assert.Equal(UserId, notepad.UserId);
Assert.Equal(UserEntity, notepad.User);
Assert.Equal(InquiryId, notepad.InquiryId);
Assert.Equal(InquiryEntity, notepad.Inquiry);
Assert.Equal(Notes, notepad.Notes);
}
[Fact]
public void TestConstructorWithAllAttributes()
{
NotepadEntity notepad = new NotepadEntity(_id,_userId,_userEntity,_inquiryId,_inquiryEntity,_notes);
Assert.Equal(_id,notepad.Id);
Assert.Equal(_userId,notepad.UserId);
Assert.Equal(_userEntity,notepad.User);
Assert.Equal(_inquiryId,notepad.InquiryId);
Assert.Equal(_inquiryEntity,notepad.Inquiry);
Assert.Equal(_notes,notepad.Notes);
NotepadEntity notepad = new NotepadEntity
{
Id = Id,
UserId = UserId,
User = UserEntity,
InquiryId = InquiryId,
Inquiry = InquiryEntity,
Notes = Notes
};
Assert.Equal(Id, notepad.Id);
Assert.Equal(UserId, notepad.UserId);
Assert.Equal(UserEntity, notepad.User);
Assert.Equal(InquiryId, notepad.InquiryId);
Assert.Equal(InquiryEntity, notepad.Inquiry);
Assert.Equal(Notes, notepad.Notes);
}
}

@ -1,14 +1,15 @@
using Entities;
namespace TestEF
{
namespace TestEF.EntitiesTests;
public class TestUserEntity
{
private const string _username = "username";
private const string _email = "example@email.com";
private const string _password = "password";
private const bool _isAdmin = true;
private const int _id = 42;
private const string Username = "username";
private const string Email = "example@email.com";
private const string Password = "password";
private const bool IsAdmin = true;
private const int Id = 42;
[Fact]
public void TestDefaultConstructor()
{
@ -23,8 +24,8 @@ namespace TestEF
[Fact]
public void TestConstructorWithOnlyId()
{
UserEntity user = new UserEntity(_id);
Assert.Equal(_id,user.Id);
UserEntity user = new UserEntity{ Id = Id};
Assert.Equal(Id, user.Id);
Assert.Null(user.Username);
Assert.Null(user.Email);
Assert.Null(user.Password);
@ -34,23 +35,35 @@ namespace TestEF
[Fact]
public void TestConstructorWithoutId()
{
UserEntity user = new UserEntity(_username, _password, _email, _isAdmin);
UserEntity user = new UserEntity
{
Username = Username,
Password = Password,
Email = Email,
IsAdmin = IsAdmin
};
Assert.Equal(0, user.Id);
Assert.Equal(_username, user.Username);
Assert.Equal(_email, user.Email);
Assert.Equal(_password, user.Password);
Assert.Equal(Username, user.Username);
Assert.Equal(Email, user.Email);
Assert.Equal(Password, user.Password);
Assert.True(user.IsAdmin);
}
[Fact]
public void TestConstructorWithoutAllAttributes()
{
UserEntity user = new UserEntity(_id, _username, _password, _email, _isAdmin);
Assert.Equal(_id,user.Id);
Assert.Equal(_username, user.Username);
Assert.Equal(_email, user.Email);
Assert.Equal(_password, user.Password);
UserEntity user = new UserEntity
{
Id = Id,
Username = Username,
Password = Password,
Email = Email,
IsAdmin = IsAdmin
};
Assert.Equal(Id, user.Id);
Assert.Equal(Username, user.Username);
Assert.Equal(Email, user.Email);
Assert.Equal(Password, user.Password);
Assert.True(user.IsAdmin);
}
}
}
Loading…
Cancel
Save