Endpoints
Przykładowa tabela w bazie danych.
public interface IPersonServices
{
Task<IEnumerable<Person>> GetPerson();
public Task<Person> GetPerson(string FirstName);
public Task<Person> CreatePerson(PersonDto person);
public Task<bool>DeletePerson(Guid id);
public Task<bool> UpdatePerson(Guid id, PersonUpdateDto personUpdateDto);
public Task<Person> GetPersonById(Guid id);
}
namespace ApiDapper.Services
{
public class PersonServices : IPersonServices
{
private readonly DapperContext _context;
private readonly IPasswordHasher<Person> _passwordHasher;
public PersonServices(DapperContext context, IPasswordHasher<Person> passwordHasher)
{
_context = context;
_passwordHasher = passwordHasher;
}
public async Task<Person> CreatePerson(PersonDto personDto)
{
var query = "INSERT INTO Person (Id, FirstName,LastName,Password,Email,dateTimeCreate) VALUES (@Id, @FirstName,@LastName,@Password,@Email,@dateTimeCreate)";
var parameters = new DynamicParameters();
Person person = new Person();
person.Id = Guid.NewGuid();
person.dateTimeCreate = DateTime.Now.Date;
person.FirstName = personDto.FirstName;
person.LastName = personDto.LastName;
person.Email = personDto.Email;
person.Password = _passwordHasher.HashPassword(person,personDto.Password);
parameters.Add("Id", person.Id, DbType.Guid);
parameters.Add("dateTimeCreate", person.dateTimeCreate, DbType.DateTime);
parameters.Add("FirstName", person.FirstName, DbType.String);
parameters.Add("LastName", person.LastName, DbType.String);
parameters.Add("Password", person.Password, DbType.String);
parameters.Add("Email", person.Email, DbType.String);
using (var connection = _context.CreateConnection())
{
await connection.ExecuteAsync(query, parameters);
}
return person;
}
public async Task<IEnumerable<Person>> GetPerson()
{
var query = "SELECT * FROM Person";
using (var connection = _context.CreateConnection())
{
var person = await connection.QueryAsync<Person>(query);
return person.ToList();
}
}
public async Task<Person> GetPerson(string FirstName)
{
var query = "SELECT * FROM Person WHERE FirstName = @FirstName";
using (var connection = _context.CreateConnection())
{
var person = await connection.QuerySingleOrDefaultAsync<Person>(query, new { FirstName });
return person;
}
}
public async Task<bool> UpdatePerson(Guid id, PersonUpdateDto personUpdateDto)
{
var query = "UPDATE Person SET FirstName = @FirstName, LastName = @LastName, Email = @Email , Password = @Password WHERE Id = @Id";
var parameters = new DynamicParameters();
Person person = new Person();
person.Password = _passwordHasher.HashPassword(person, personUpdateDto.Password);
parameters.Add("Id", id, DbType.Guid);
parameters.Add("FirstName", personUpdateDto.FirstName, DbType.String);
parameters.Add("LastName", personUpdateDto.LastName, DbType.String);
parameters.Add("Email", personUpdateDto.Email, DbType.String);
parameters.Add("Password", person.Password, DbType.String);
using (var connection = _context.CreateConnection())
{
await connection.ExecuteAsync(query, parameters);
}
return true;
}
public async Task<bool> DeletePerson(Guid id)
{
var query = "DELETE FROM Person WHERE Id = @Id";
using (var connection = _context.CreateConnection())
{
await connection.ExecuteAsync(query, new { id });
}
return true;
}
public async Task<Person> GetPersonById(Guid id)
{
var query = "SELECT * FROM Person WHERE Id = @id";
using (var connection = _context.CreateConnection())
{
var person = await connection.QuerySingleOrDefaultAsync<Person>(query, new { id });
return person;
}
}
}
}
IPersonServices.cs i PersonServices.cs
using ApiDapper.Models;
using ApiDapper.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace ApiDapper.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PersonController : ControllerBase
{
private readonly IPersonServices _personServices;
public PersonController(IPersonServices personServices)
{
_personServices = personServices;
}
[HttpGet]
public async Task<ActionResult> GetPerson()
{
try
{
var persons = await _personServices.GetPerson();
return Ok(persons);
}
catch (Exception ex)
{
//log error
return StatusCode(500, ex.Message);
}
}
[HttpGet("PersonByFirstName")]
public async Task<ActionResult> GetPerson(string name)
{
try
{
var person = await _personServices.GetPerson(name);
if (person == null)
return NotFound();
return Ok(person);
}
catch (Exception ex)
{
//log error
return StatusCode(500, ex.Message);
}
}
[HttpPost("AddPerson")]
public async Task<ActionResult> AddPerson(PersonDto personDto)
{
try
{
var createdPerson = await _personServices.CreatePerson(personDto);
return Ok(createdPerson);
}
catch (Exception ex)
{
//log error
return StatusCode(500, ex.Message);
}
}
[HttpPut("UbdatePerson")]
public async Task<ActionResult> UpdatePerson(Guid id, PersonUpdateDto personUpdateDto)
{
try
{
var dbCompany = await _personServices.GetPersonById(id);
if (dbCompany == null)
return NotFound();
await _personServices.UpdatePerson(id, personUpdateDto);
return NoContent();
}
catch (Exception ex)
{
//log error
return StatusCode(500, ex.Message);
}
}
[HttpDelete("DeletePerson")]
public async Task<ActionResult> DeletePerson(Guid id)
{
try
{
var dbCompany = await _personServices.GetPersonById(id);
if (dbCompany == null)
return NotFound();
await _personServices.DeletePerson(id);
return NoContent();
}
catch (Exception ex)
{
//log error
return StatusCode(500, ex.Message);
}
}
}
}
PersonController.cs
using ApiDapper.Controllers;
using ApiDapper.Models;
using ApiDapper.Services;
using Dapper.Models;
using Microsoft.AspNetCore.Http.HttpResults;
using Moq;
namespace ApiDapper.Tests
{
public class PersonControllerTest
{
private static List<Person> GetFakePersonList()
{
return new List<Person>()
{
new Person
{
Id = Guid.NewGuid(),
dateTimeCreate = DateTime.Now,
Email = "email@gmail.com",
FirstName = "xxx",
LastName = "xxx",
Password = "123123"
}
};
}
[Fact]
public async Task TestGetPersonReturnPerson()
{
var mocGetPerson = new Mock<IPersonServices>();
mocGetPerson.Setup(m => m.GetPerson().Result).Returns(PersonControllerTest.GetFakePersonList);
var result = mocGetPerson.Object.GetPerson();
Assert.NotNull(result);
}
[Fact]
public async Task TestGetPersonByNameReturnPerson()
{
Person pers = new Person
{
Id = Guid.NewGuid(),
dateTimeCreate = DateTime.Now,
Email = "email@gmail.com",
FirstName = "xxx",
LastName = "xxx",
Password = "123123"
};
var mocGetPersonByName = new Mock<IPersonServices>();
mocGetPersonByName.Setup(m => m.GetPerson("xxx").Result).Returns(pers);
var result = mocGetPersonByName.Object.GetPerson("xxx");
Assert.NotNull(result);
}
[Fact]
public async Task TestCreatePersonReturnPerson()
{
PersonDto pers = new PersonDto
{
Email = "email@gmail.com",
FirstName = "xxx",
LastName = "xxx",
Password = "123123"
};
Person perss = new Person
{
Id = Guid.NewGuid(),
dateTimeCreate = DateTime.Now,
Email = "email@gmail.com",
FirstName = "xxx",
LastName = "xxx",
Password = "123123"
};
var mocGetPersonByName = new Mock<IPersonServices>();
mocGetPersonByName.Setup(m => m.CreatePerson(pers).Result).Returns(perss);
var result = mocGetPersonByName.Object.CreatePerson(pers);
Assert.Equal(perss.FirstName, result.Result.FirstName);
}
[Fact]
public async Task TestGetPersonByIdReturnPerson()
{
Person pers = new Person
{
Id = Guid.NewGuid(),
dateTimeCreate = DateTime.Now,
Email = "email@gmail.com",
FirstName = "xxx",
LastName = "xxx",
Password = "123123"
};
var mocGetPersonByName = new Mock<IPersonServices>();
mocGetPersonByName.Setup(m => m.GetPersonById(pers.Id).Result).Returns(pers);
var result = mocGetPersonByName.Object.GetPersonById(pers.Id);
Assert.Equal("xxx", result.Result.FirstName);
}
[Fact]
public async Task TestDeletePersonReturnTrue()
{
Person pers = new Person
{
Id = Guid.NewGuid(),
dateTimeCreate = DateTime.Now,
Email = "email@gmail.com",
FirstName = "xxx",
LastName = "xxx",
Password = "123123"
};
var mocGetPersonByName = new Mock<IPersonServices>();
mocGetPersonByName.Setup(m => m.DeletePerson(pers.Id).Result).Returns(true);
var result = mocGetPersonByName.Object.DeletePerson(pers.Id);
Assert.True(result.Result);
}
[Fact]
public async Task TestUpdatePersonReturnTrue()
{
PersonUpdateDto persUpDto = new PersonUpdateDto
{
Email = "email@gmail.com",
FirstName = "xxxx",
LastName = "xxxx",
Password = "1231231"
};
Person pers = new Person
{
Id = Guid.NewGuid(),
dateTimeCreate = DateTime.Now,
Email = "email@gmail.com",
FirstName = "xxx",
LastName = "xxx",
Password = "123123"
};
var mocGetPersonByName = new Mock<IPersonServices>();
mocGetPersonByName.Setup(m => m.UpdatePerson(pers.Id,persUpDto).Result).Returns(true);
var result = mocGetPersonByName.Object.UpdatePerson(pers.Id, persUpDto);
Assert.True(result.Result);
}
}
}
Testy w Xunit + moq.