mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
MC #2 User Controller (Add, Update, Get, Delete)
This commit is contained in:
parent
c6df9203d5
commit
68e0338fe7
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -37,7 +37,7 @@ namespace MyCore.Controllers
|
||||
|
||||
if (IsValidUserAndPasswordCombination(email, password))
|
||||
{
|
||||
UserInfo user = _userService.GetUser(email);
|
||||
UserInfo user = _userService.GetUserByEmail(email);
|
||||
user.Token = _tokenService.GenerateToken(email).ToString();
|
||||
|
||||
return user;
|
||||
@ -48,8 +48,19 @@ namespace MyCore.Controllers
|
||||
private bool IsValidUserAndPasswordCombination(string email, string password)
|
||||
{
|
||||
// Test if is database and is correct
|
||||
if (email == "thomas.fransolet@hotmail.be" && password == "MonsieurMagic") { return true; }
|
||||
else return false;
|
||||
List<UserInfo> users = _userService.GetUsers();
|
||||
|
||||
UserInfo user = users.Where(u => u.Email == email).FirstOrDefault();
|
||||
|
||||
if (user != null && user.Password == password)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
/*if (email == "thomas.fransolet@hotmail.be" && password == "MonsieurMagic") { return true; }
|
||||
else return false;*/
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -47,12 +47,18 @@ namespace MyCore.Controllers
|
||||
[HttpGet("{id}")]
|
||||
public ActionResult<UserInfo> Get(string id)
|
||||
{
|
||||
UserInfo user = new UserInfo();
|
||||
user.FirstName = "Thomas";
|
||||
user.Id = "01";
|
||||
return user;
|
||||
//return _userService.GetUser(id);
|
||||
if (id != null)
|
||||
{
|
||||
List<UserInfo> users = _userService.GetUsers();
|
||||
|
||||
if (!users.Select(u => u.Id).Contains(id))
|
||||
{
|
||||
return Conflict("This user was not found");
|
||||
}
|
||||
|
||||
return _userService.GetUserById(id);
|
||||
}
|
||||
return StatusCode(500);
|
||||
}
|
||||
|
||||
// POST: User/Create
|
||||
@ -66,6 +72,15 @@ namespace MyCore.Controllers
|
||||
if (newUser != null)
|
||||
{
|
||||
newUser.Token = _tokenService.GenerateToken(newUser.Email).ToString();
|
||||
newUser.DateCreation = DateTime.Now;
|
||||
|
||||
List<UserInfo> users = _userService.GetUsers();
|
||||
|
||||
if (users.Select(u => u.Email).Contains(newUser.Email))
|
||||
{
|
||||
return Conflict("This Email is already used");
|
||||
}
|
||||
|
||||
UserInfo userCreated = _userService.CreateUser(newUser);
|
||||
|
||||
return userCreated;
|
||||
@ -73,26 +88,49 @@ namespace MyCore.Controllers
|
||||
return StatusCode(500);
|
||||
}
|
||||
|
||||
/*
|
||||
// POST api/values
|
||||
[HttpPost]
|
||||
public void Post([FromBody] string value)
|
||||
|
||||
// PUT: User/Update
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
[HttpPut]
|
||||
public ActionResult<UserInfo> UpdateUser([FromBody] UserInfo updatedUser)
|
||||
{
|
||||
// For more information on protecting this API from Cross Site Request Forgery (CSRF) attacks, see https://go.microsoft.com/fwlink/?LinkID=717803
|
||||
if (updatedUser != null)
|
||||
{
|
||||
List<UserInfo> users = _userService.GetUsers();
|
||||
|
||||
if (!users.Select(u => u.Email).Contains(updatedUser.Email))
|
||||
{
|
||||
return NotFound("The user was not found");
|
||||
}
|
||||
|
||||
UserInfo userModified = _userService.Update(updatedUser.Id, updatedUser);
|
||||
|
||||
return userModified;
|
||||
}
|
||||
return StatusCode(500);
|
||||
}
|
||||
|
||||
// PUT api/values/5
|
||||
[HttpPut("{id}")]
|
||||
public void Put(int id, [FromBody] string value)
|
||||
{
|
||||
// For more information on protecting this API from Cross Site Request Forgery (CSRF) attacks, see https://go.microsoft.com/fwlink/?LinkID=717803
|
||||
}
|
||||
|
||||
// DELETE api/values/5
|
||||
// DELETE: User/Delete
|
||||
[HttpDelete("{id}")]
|
||||
public void Delete(int id)
|
||||
public ActionResult DeleteUser(string id)
|
||||
{
|
||||
// For more information on protecting this API from Cross Site Request Forgery (CSRF) attacks, see https://go.microsoft.com/fwlink/?LinkID=717803
|
||||
}*/
|
||||
if (id != null)
|
||||
{
|
||||
List<UserInfo> users = _userService.GetUsers();
|
||||
|
||||
if (!users.Select(u => u.Id).Contains(id))
|
||||
{
|
||||
return NotFound("The user was not found");
|
||||
}
|
||||
|
||||
_userService.Remove(id);
|
||||
|
||||
return Accepted("The user has been deleted");
|
||||
}
|
||||
return StatusCode(500);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,7 +33,10 @@ namespace MyCore.Models
|
||||
public string Token { get; set; }
|
||||
|
||||
[BsonElement("Birthday")]
|
||||
public string Birthday { get; set; }
|
||||
public DateTime Birthday { get; set; }
|
||||
|
||||
[BsonElement("DateCreation")]
|
||||
public DateTime DateCreation { get; set; }
|
||||
|
||||
[BsonElement("Address")]
|
||||
public string Address { get; set; }
|
||||
@ -44,6 +47,9 @@ namespace MyCore.Models
|
||||
[BsonElement("State")]
|
||||
public string State { get; set; }
|
||||
|
||||
[BsonElement("Country")]
|
||||
public string Country { get; set; }
|
||||
|
||||
[BsonElement("Language")]
|
||||
public string Language { get; set; }
|
||||
|
||||
|
||||
@ -25,30 +25,32 @@ namespace MyCore.Services
|
||||
return _Users.Find(m => true).ToList();
|
||||
}
|
||||
|
||||
public UserInfo GetUser(string email)
|
||||
public UserInfo GetUserByEmail(string email)
|
||||
{
|
||||
return _Users.Find<UserInfo>(m => m.Email == email).FirstOrDefault();
|
||||
}
|
||||
|
||||
public UserInfo GetUserById(string id)
|
||||
{
|
||||
return _Users.Find<UserInfo>(m => m.Id == id).FirstOrDefault();
|
||||
}
|
||||
|
||||
public UserInfo CreateUser(UserInfo user)
|
||||
{
|
||||
_Users.InsertOne(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
/*public void Update(string id, Book bookIn)
|
||||
public UserInfo Update(string id, UserInfo userIn)
|
||||
{
|
||||
_books.ReplaceOne(book => book.Id == id, bookIn);
|
||||
}
|
||||
|
||||
public void Remove(Book bookIn)
|
||||
{
|
||||
_books.DeleteOne(book => book.Id == bookIn.Id);
|
||||
_Users.ReplaceOne(user => user.Id == id, userIn);
|
||||
return userIn;
|
||||
}
|
||||
|
||||
public void Remove(string id)
|
||||
{
|
||||
_books.DeleteOne(book => book.Id == id);
|
||||
}*/
|
||||
_Users.DeleteOne(user => user.Id == id);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -72,6 +72,11 @@
|
||||
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:MyCore.Controllers.UserController.UpdateUser(MyCore.Models.UserInfo)">
|
||||
<summary>
|
||||
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:MyCore.Controllers.ValuesController.Get">
|
||||
<summary>
|
||||
It's a test ! :)
|
||||
|
||||
@ -72,6 +72,11 @@
|
||||
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:MyCore.Controllers.UserController.UpdateUser(MyCore.Models.UserInfo)">
|
||||
<summary>
|
||||
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:MyCore.Controllers.ValuesController.Get">
|
||||
<summary>
|
||||
It's a test ! :)
|
||||
|
||||
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user