dotnet add package AutoMapper --version 13.0.1
dotnet add package Microsoft.AspNetCore.OpenApi --version 8.0.4
dotnet add package Microsoft.EntityFrameworkCore --version 8.0.4
dotnet add package Microsoft.EntityFrameworkCore.Design --version 8.0.4
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 8.0.4
dotnet add package Swashbuckle.AspNetCore --version 6.5.0
AutoMapper
Using AutoMapper for endpoints
namespace ToDoApp_App.Models;
public class User{
[Key]
[JsonPropertyName("id")]
public int Id { get; set; } = 0;
[JsonPropertyName("password")]
public string Password{get; set;} = string.Empty;
[JsonPropertyName("mail")]
public string Mail { get; set; } = string.Empty;
[JsonPropertyName("firstName")]
public string FirtstName { get; set; } = string.Empty;
[JsonPropertyName("lastName")]
public string LastName{get;set;} = string.Empty;
[JsonPropertyName("Note")]
public ICollection<Note> Note{get; set;} = new List<Note>();
}
namespace ToDoApp_API.Dto
{
public class UserDto
{
[JsonPropertyName("password")]
public string Password { get; set; } = string.Empty;
[JsonPropertyName("mail")]
public string Mail { get; set; } = string.Empty;
[JsonPropertyName("firstName")]
public string FirtstName { get; set; } = string.Empty;
[JsonPropertyName("lastName")]
public string LastName { get; set; } = string.Empty;
}
}
</details>
namespace ToDoApp_API.Helpers
{
public class MappingProfile : Profile
{
public MappingProfile()
{
CreateMap<User, UserDto>();
CreateMap<UserDto , User>();
}
}
}
In this method we use AutoMapper for create storage, because we wanted the user only have the needed to write the necessary camps, we receive an UserDto and convert into a User, that way we can insert the user into DB then return the user but as a UserDtop, that way we can control what we are returning. in this case I donβt want to return the Id.
[HttpPost]
public async Task<IActionResult> CreateUserAsync(UserDto user)
{
try
{
//AutoMapper part --------------------------------------
User userMapper = _mapper.Map<User>(user);
UserDto queryResult = _mapper.Map<UserDto>
(await _userService.CreateUserAsync(userMapper));
//------------------------------------------------------
return Ok(queryResult);
}
catch (AppValidationException error)
{
return BadRequest($"Ops! Valitadion Error: {error.Message}");
}
catch (DbOperationException error)
{
return BadRequest($"Ops! Someting went wrong in DB Operation: {error.Message}");
}
}
User functions: Delete account, update user info, create a new user
these are the methods for user, all of them that update the DB return a bool in that way we can take the result of the operation and if it was true return the actualization in the services exept for DeletUserAsync.
Look the following example
using ToDoApp_App.Models;
namespace ToDoApp_API.Interfaces;
public interface IUserRepository{
// GETTERS
Task<ICollection<User>> GetUsersAsync();
Task<User> GetUserAsync(string mail);
//SETTERS
Task<bool> CreateUserAsync(User OneUser);
//UPDATES
Task<bool> UpdateUserMailAsync(string mail, string targetMail);
Task<bool> UpdateUserPasswordAsync(string password, string mail);
Task<bool> UpdateUserFirstNameAsync(string firtstName, string mail);
Task<bool> UpdateUserLastNameAsync(string lastName, string mail);
//DELETS
//For delete the acount
Task<bool> DeleteUserAsync(string password, string mail);
//MORE
Task<bool> ExistUserAsyn(string mail);
}
public async Task<bool> CreateUserAsync(User OneUser)
{
if (await ExistUserAsyn(OneUser.Mail))
throw new DbOperationException("Couldn't insert. Account already exist.");
try
{
var queryResult = await _dbContext
.AddAsync(OneUser);
if (queryResult is not null)
{
await _dbContext.SaveChangesAsync();
return true;
}
return false;
}
catch (DbUpdateException error)
{
throw new DbOperationException(error.Message);
}
}
public async Task<User> CreateUserAsync(User user)
{
if (user.Password.Length <= 8 ||
user.Mail.Length <= 8 ||
user.FirtstName.Length <= 3 ||
user.LastName.Length <= 3)
{
throw new AppValidationException("One of the fields haven't correct format");
}
bool queryOperation = await _userRepository
.CreateUserAsync(user);
if (!queryOperation)
throw new AppValidationException("Operation executed but wasn't changes");
return await GetUserAsync(user.Mail);
}
Create DB
Stright to the point, simple DB for the very basic functions
CREATE TABLE "User"(
"Id" INT IDENTITY(1,1) PRIMARY KEY,
"Password" VARCHAR(50) NOT NULL,
"Mail" VARCHAR(50) NOT NULL,
"FirtstName" VARCHAR(50) NOT NULL,
"LastName" VARCHAR(50) NOT NULL,
"Premiun" TINYINT NOT NULL,
"PaymentDate" DATETIME NOT NULL,
"Tutor" TINYINT NOT NULL
);
CREATE TABLE "Message"(
"Id" INT IDENTITY(1,1) PRIMARY KEY,
"Content" VARCHAR(255) NOT NULL,
"IdUserClassroom" INT NOT NULL,
"SendDate" DATETIME NOT NULL,
FOREIGN KEY("IdUserClassroom") REFERENCES "UsersClassroom"("Id")
);
CREATE TABLE "Tutor"(
"Id" INT IDENTITY(1,1) PRIMARY KEY,
"IdUser" INT NOT NULL,
"IdClassroom" INT NOT NULL,
FOREIGN KEY("IdUser") REFERENCES "User"("Id"),
FOREIGN KEY("IdClassroom") REFERENCES "Classroom"("Id")
);
CREATE TABLE "Classroom"(
"Id" INT IDENTITY(1,1) PRIMARY KEY,
"Block" VARCHAR(10) NOT NULL,
"Classroom" INT NOT NULL
);
CREATE TABLE "ClassroomForClass"(
"Id" INT IDENTITY(1,1) PRIMARY KEY,
"StartDate" DATETIME NOT NULL,
"EndDate" DATETIME NOT NULL,
"Block" VARCHAR(10) NOT NULL,
"Classroom" INT NOT NULL
);
CREATE TABLE "UsersClassroom"(
"Id" INT IDENTITY(1,1) PRIMARY KEY,
"IdClassroom" INT NOT NULL,
"IdUser" INT NOT NULL,
"StartDate" DATETIME NOT NULL,
"EndDate" DATETIME NOT NULL,
"IdConversation" INT NOT NULL,
FOREIGN KEY("IdClassroom") REFERENCES "Classroom"("Id"),
FOREIGN KEY("IdUser") REFERENCES "User"("Id")
);
CREATE TABLE "UserDetails"(
"id" INT IDENTITY(1,1) PRIMARY KEY,
"UserId" INT NOT NULL,
"Card" INT NOT NULL,
"CSV" SMALLINT NOT NULL,
"Date" VARCHAR(6) NOT NULL,
"Proprietary" VARCHAR(50) NOT NULL,
"CardType" VARCHAR(25) NOT NULL,
"PaymentInfo" VARCHAR(255) NOT NULL,
FOREIGN KEY("UserId") REFERENCES "User"("Id")
);
CREATE TABLE "Friends"(
"Id" INT IDENTITY(1,1) PRIMARY KEY,
"IdUser" INT NOT NULL,
"IdFriend" INT NOT NULL,
FOREIGN KEY("IdUser") REFERENCES "User"("Id"),
FOREIGN KEY("IdFriend") REFERENCES "User"("Id")
);