Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions Foodmoji.Api/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ public async Task<IActionResult> createUserAccount([FromBody] RegisterAccountVie
[Route("login")]
public async Task<IActionResult> userLogin([FromBody] LoginViewModel account)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
try
{

//String userPassword = password.hashPassword(account.Password);
var dbUser = _accountRepo.Where(user => user.Email == account.Email && user.Password == account.Password).Select(user => new
{
Expand Down Expand Up @@ -97,15 +99,16 @@ public async Task<IActionResult> userLogin([FromBody] LoginViewModel account)

[HttpPut]
[Route("updateUserAccount")]
public async Task<IActionResult> updateUserAccount([FromBody] RegisterAccountViewModel account)
public async Task<IActionResult> updateUserAccount([FromBody] UpdateAccountViewModel model)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
try
{
var userAccount = _accountRepo.FindAll();
var userAccount = _accountRepo.Where(x => x.Id == model.Id).FirstOrDefault();

if (userAccount != null)
{
var updateAccount = account.ToEntity();
var updateAccount = model.ToEntity(userAccount);
_accountRepo.Update(updateAccount);
_accountRepo.Save();

Expand All @@ -131,7 +134,33 @@ public async Task<IActionResult> updateUserAccount([FromBody] RegisterAccountVie
[Route("getAllAccounts")]
public async Task<IActionResult> getAllAccounts()
{
return Ok(_accountRepo.FindAll());
try
{

return Ok(_accountRepo.FindAll);
}
catch (Exception)
{
return StatusCode(StatusCodes.Status500InternalServerError, "Error retrieving data from the database");
}

}

[HttpGet]
[Route("userAccount/{id}")]
public async Task<IActionResult> getId(int id)
{

return Ok(new {userID = id});
}

[HttpGet]
[Route("Users/currentUser")]
public async Task<IActionResult> getLoggedInUserID()
{
int id = Convert.ToInt32(HttpContext.User.FindFirstValue("userID"));

return Ok(new { userID = id });
}

}
Expand Down
11 changes: 11 additions & 0 deletions Foodmoji_Application/Helpers/Extensions/AccountExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,16 @@ public static Account ToEntity(this RegisterAccountViewModel model)
Password = model.Password
};
}

public static Account ToEntity(this UpdateAccountViewModel model, Account entity)
{
entity.FirstName = model.FirstName;
entity.Surname = model.Surname;
entity.Email = model.Email;
entity.PhoneNumber = model.PhoneNumber;
entity.Password = model.Password;

return entity;
}
}
}
30 changes: 30 additions & 0 deletions Foodmoji_Application/ViewModels/Account/UpdateAccountViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Foodmoji_Application.ViewModels.Account
{
public class UpdateAccountViewModel
{
public int Id { get; set; }

[MaxLength(50), Required(ErrorMessage = "Firstname is required")]
public string FirstName { get; set; }

[MaxLength(50), Required(ErrorMessage = "Surname is required")]
public string Surname { get; set; }

[MaxLength(100)]
[Required, EmailAddress(ErrorMessage = "Email address is required, please enter a valid email address")]
public string Email { get; set; }

[MaxLength(15), Phone(ErrorMessage = "Please enter a valid phone number")]
public string PhoneNumber { get; set; }

[MaxLength(6), Required(ErrorMessage = "Please enter a password with maximum of 6 characters")]
public string Password { get; set; }
}
}
29 changes: 29 additions & 0 deletions Foodmoji_Application/ViewModels/Account/getAllAccountsViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Foodmoji_Application.ViewModels.Account
{
public class getAllAccountsViewModel
{
[MaxLength(50), Required(ErrorMessage = "Firstname is required")]
public string FirstName { get; set; }

[MaxLength(50), Required(ErrorMessage = "Surname is required")]
public string Surname { get; set; }

[MaxLength(100)]
[Required, EmailAddress(ErrorMessage = "Email address is required, please enter a valid email address")]
public string Email { get; set; }

[MaxLength(15), Phone(ErrorMessage = "Please enter a valid phone number")]
public string PhoneNumber { get; set; }

[MaxLength(6), Required(ErrorMessage = "Please enter a password with maximum of 6 characters")]
public string Password { get; set; }

}
}
5 changes: 2 additions & 3 deletions Foodmoji_Domain/Models/Account.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

namespace Foodmoji_Domain.Models
{
public class Account
public class Account : AuditEntity<int>
{
[Key]
public int Id { get; set; }


[MaxLength(50), Required(ErrorMessage = "Firstname is required")]
public string FirstName { get; set; }
Expand Down
6 changes: 2 additions & 4 deletions Foodmoji_Domain/Models/AccountRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@

namespace Foodmoji_Domain.Models
{
public class AccountRole
public class AccountRole : AuditEntity<int>
{
[Key]

public int Id { get; set; }

public int AccountId { get; set; }
public virtual Account Account { get; set; }
// Public List<Account> Accounts { get: set} = new List<Account>(); lookup
Expand Down
5 changes: 2 additions & 3 deletions Foodmoji_Domain/Models/Customer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace Foodmoji_Domain.Models
{
public class Customer
public class Customer : AuditEntity<int>
{
[Key]
public int Id { get; set; }


[MaxLength(100), Required(ErrorMessage = "Name is required")]
public string Firstname { get; set; }
Expand Down
5 changes: 2 additions & 3 deletions Foodmoji_Domain/Models/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@

namespace Foodmoji_Domain.Models
{
public class Order
public class Order : AuditEntity<int>
{
[Key]
public int Id { get; set; }

public DateTime Date { get; set; }
[Required(ErrorMessage = "Please enter the quantity for this order")]
public int Quantity { get; set; }
Expand Down
5 changes: 2 additions & 3 deletions Foodmoji_Domain/Models/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace Foodmoji_Domain.Models
{
public class Product
public class Product : AuditEntity<int>
{
[Key]
public int Id { get; set; }


[Required (ErrorMessage = "Please provide a valid product name")]
public string ProductName { get; set; }
Expand Down
5 changes: 2 additions & 3 deletions Foodmoji_Domain/Models/Role.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@

namespace Foodmoji_Domain.Models
{
public class Role
public class Role : AuditEntity<int>
{
[Key]
public int Id { get; set; }

[MaxLength(100), Required(ErrorMessage = "Nmae is required")]
public string Name { get; set; }

Expand Down