Add a controler for "User" (add and authenticate)

This commit is contained in:
Sébastien André
2020-03-12 12:24:51 +01:00
parent 8a29fd7de9
commit 32a27b6d26
23 changed files with 225 additions and 34 deletions

View File

@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Business.Interface
{
public interface IAircraftService

View File

@@ -1,6 +1,8 @@
using System.Collections.Generic;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Business.Interface
{
public interface IDropZoneService

View File

@@ -1,6 +1,8 @@
using System.Collections.Generic;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Business.Interface
{
public interface IGearService
@@ -11,8 +13,8 @@ namespace skydiveLogs_api.Business.Interface
void DeleteGearById(int id);
void UpdateGear(int id, Gear Gear);
void UpdateGear(int id, Gear gear);
void AddNewGear(Gear Gear);
void AddNewGear(Gear gear);
}
}

View File

@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Business.Interface
{
public interface IJumpService

View File

@@ -1,7 +1,7 @@
using skydiveLogs_api.Model;
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Business.Interface
{

View File

@@ -0,0 +1,11 @@
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Business.Interface
{
public interface IUserService
{
User GetByLogin(string login, string password);
void AddNewUser(User user);
}
}

View File

@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using skydiveLogs_api.Business.Interface;
using skydiveLogs_api.Model;
using skydiveLogs_api.Data.Interface;
namespace skydiveLogs_api.Business
{
public class UserService : IUserService
{
public UserService(IUserRepository userRepository)
{
_userRepository = userRepository;
}
public User GetByLogin(string login, string password)
{
var tmp = _userRepository.GetByLogin(login, password);
return tmp;
}
public void AddNewUser(User newUser)
{
_userRepository.Add(newUser);
}
private readonly IUserRepository _userRepository;
}
}

View File

@@ -20,5 +20,7 @@ namespace skydiveLogs_api.Data.Interface
ILiteCollection<JumpType> CollOfJumpType { get; }
ILiteCollection<Jump> CollOfJump { get; }
ILiteCollection<User> CollOfUser { get; }
}
}

View File

@@ -0,0 +1,10 @@
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Data.Interface
{
public interface IUserRepository : IRepository<User>
{
User GetByLogin(string login, string password);
}
}

View File

@@ -37,5 +37,7 @@ namespace skydiveLogs_api.Data
public ILiteCollection<JumpType> CollOfJumpType => _db.GetCollection<JumpType>();
public ILiteCollection<Jump> CollOfJump => _db.GetCollection<Jump>();
public ILiteCollection<User> CollOfUser => _db.GetCollection<User>();
}
}

View File

@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using LiteDB;
using skydiveLogs_api.Data.Interface;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Data
{
public class UserRepository : IUserRepository
{
public UserRepository(IDataProvider dataProvider)
{
_dataProvider = dataProvider;
_col = _dataProvider.CollOfUser;
}
public User GetByLogin(string login, string password)
{
return _col.FindOne(u => u.Login == login && u.Password == password);
}
public bool Add(User newUser)
{
var result = true;
try
{
_col.Insert(newUser);
}
catch
{
result = false;
}
return result;
}
public IEnumerable<User> GetAll()
{
throw new NotImplementedException();
}
public User GetById(int id)
{
throw new NotImplementedException();
}
public bool Update(User updated)
{
throw new NotImplementedException();
}
private readonly IDataProvider _dataProvider;
private readonly ILiteCollection<User> _col;
}
}

View File

@@ -6,7 +6,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LiteDB" Version="5.0.1" />
<PackageReference Include="LiteDB" Version="5.0.3" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,15 @@
namespace skydiveLogs_api.Model
{
public class User
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Login { get; set; }
public string Password { get; set; }
}
}

View File

@@ -0,0 +1,44 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Cors;
using AutoMapper;
using skydiveLogs_api.Business.Interface;
using skydiveLogs_api.DataContract;
using skydiveLogs_api.Model;
namespace skydiveLogs_api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
public UserController(IUserService userService,
IMapper mapper)
{
_userService = userService;
_mapper = mapper;
}
// POST: api/User
[HttpPost]
[EnableCors]
public UserResp Authenticate([FromBody] string login, [FromBody] string password)
{
var result = _userService.GetByLogin(login, password);
return _mapper.Map<UserResp>(result);
}
// POST: api/User
[HttpPost]
[EnableCors]
public void Post([FromBody] UserReq value)
{
_userService.AddNewUser(_mapper.Map<User>(value));
}
private readonly IUserService _userService;
private readonly IMapper _mapper;
}
}

Binary file not shown.

View File

@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace skydiveLogs_api.DataContract
namespace skydiveLogs_api.DataContract
{
public class AircraftResp
{

View File

@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace skydiveLogs_api.DataContract
{

View File

@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace skydiveLogs_api.DataContract
namespace skydiveLogs_api.DataContract
{
public class JumpTypeReq
{

View File

@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace skydiveLogs_api.DataContract
namespace skydiveLogs_api.DataContract
{
public class JumpTypeResp
{

View File

@@ -0,0 +1,13 @@
namespace skydiveLogs_api.DataContract
{
public class UserReq
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Login { get; set; }
public string Password { get; set; }
}
}

View File

@@ -0,0 +1,13 @@
namespace skydiveLogs_api.DataContract
{
public class UserResp
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Login { get; set; }
}
}

View File

@@ -11,6 +11,7 @@ namespace skydiveLogs_api.Mapper
CreateMap<DataContract.AircraftReq, Model.Aircraft>();
CreateMap<DataContract.DropZoneReq, Model.DropZone>();
CreateMap<DataContract.GearReq, Model.Gear>();
CreateMap<DataContract.UserReq, Model.User>();
CreateMap<Model.Gear, DataContract.GearResp>();
CreateMap<Model.Jump, DataContract.JumpResp>();
@@ -18,6 +19,7 @@ namespace skydiveLogs_api.Mapper
CreateMap<Model.Aircraft ,DataContract.AircraftResp>();
CreateMap<Model.DropZone ,DataContract.DropZoneResp>();
CreateMap<Model.Statistic ,DataContract.StatisticResp>();
CreateMap<Model.User, DataContract.UserResp>();
CreateMap<Model.SimpleSummary, DataContract.SimpleSummaryResp>();
}