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
2 changes: 2 additions & 0 deletions BLL/Interfaces/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public interface IUserService
Task<List<UserDTO>> GetAllUsersAsync();
Task<UserDTO> GetByEmailAndByPasswordAsync(string Email, string Password);
Task<UserDTO> UpdateAsync(UserDTO User);
Task<IEnumerable<UserDTO>> GetPendingUsersAsync();

}
}
7 changes: 7 additions & 0 deletions BLL/Services/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL.Repositories;

namespace BLL.Services
{
Expand Down Expand Up @@ -116,5 +117,11 @@ public async Task<UserDTO> UpdateAsync(UserDTO e)
throw;
}
}
public async Task<IEnumerable<UserDTO>> GetPendingUsersAsync()
{
var pendingUsers = await UserRepository.GetPendingUsersAsync();
return mapper.Map<IEnumerable<UserDTO>>(pendingUsers);
}

}
}
1 change: 1 addition & 0 deletions DAL/Interfaces/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public interface IUserRepository
Task<User> UpdateAsync(User entity);
Task<User> AddAsync(User entity);
Task DeleteAsync(int id);
Task<IEnumerable<User>> GetPendingUsersAsync();
}
}
13 changes: 12 additions & 1 deletion DAL/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,19 @@ public class User
public string Email { get; set; }
[Required]
public string Password { get; set; }

[Required]
public UserStatus Status { get; set; }
public virtual ICollection<Discussion> Discussions { get; set; }
public virtual ICollection<Comment> Comments { get; set; }

}
public enum UserStatus
{
SimpleUser,
PendingApproval,
Approved,
LoggedIn,
Admin
}

}
6 changes: 6 additions & 0 deletions DAL/Repositories/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,11 @@ public async Task<User> UpdateAsync(User entity)
throw;
}
}
public async Task<IEnumerable<User>> GetPendingUsersAsync()
{
return await context.Users.Where(u => u.Status == UserStatus.PendingApproval).ToListAsync();
}


}
}
5 changes: 3 additions & 2 deletions DTO/classes/User.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using DAL.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
Expand All @@ -13,6 +14,6 @@ public class UserDTO
public required string Username { get; set; }
public string Email { get; set; }
public string Password { get; set; }

public UserStatus Status { get; set; }
}
}
52 changes: 49 additions & 3 deletions WebApi/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
using System.Threading.Tasks;
using System;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Authorization;
using DAL.Models;
using BLL.Services;

namespace WebApi.Controllers
{
Expand All @@ -23,8 +26,8 @@ public UserController(IUserService service, ILogger<string> logger)
this.logger = logger;
}


[HttpGet]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> GetAll()
{
try
Expand All @@ -35,7 +38,7 @@ public async Task<IActionResult> GetAll()
catch (Exception ex)
{
logger.LogError("Failed to get all users: " + ex.Message);
return StatusCode(500, "Internal Server Error"); // HTTP 500 Internal Server Error
return StatusCode(500, "Internal Server Error");// HTTP 500 Internal Server Error
}
}

Expand Down Expand Up @@ -64,10 +67,13 @@ public async Task<IActionResult> GetByEmailAndPassword(string email, string pass
try
{
var user = await UserService.GetByEmailAndByPasswordAsync(email, password);
if (user == null)
if (user == null || user.Status != UserStatus.Approved)
{
return NotFound("User not found with provided email and password"); // HTTP 404 Not Found
}

user.Status= UserStatus.LoggedIn;
await UserService.UpdateAsync(user);
return Ok(user); // HTTP 200 OK
}
catch (Exception ex)
Expand All @@ -87,6 +93,7 @@ public async Task<IActionResult> Add([FromBody] UserDTO newUser)
return BadRequest("User cannot be null"); // HTTP 400 Bad Request
}

newUser.Status = UserStatus.PendingApproval;
await UserService.AddNewUserAsync(newUser);
return CreatedAtAction(nameof(GetById), new { id = newUser.Id }, newUser); // HTTP 201 Created
}
Expand All @@ -101,6 +108,45 @@ public async Task<IActionResult> Add([FromBody] UserDTO newUser)
return StatusCode(500, "Internal Server Error"); // HTTP 500 Internal Server Error
}
}
[HttpGet("pending")]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> GetPendingUsers()
{
try
{
var pendingUsers = await UserService.GetPendingUsersAsync();
return Ok(pendingUsers);
}
catch (Exception ex)
{
logger.LogError("Failed to get pending users: " + ex.Message);
return StatusCode(500, "Internal Server Error");
}
}

[HttpPut("approve/{id}")]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> ApproveUser(int id)
{
try
{
var user = await UserService.GetByIdAsync(id);
if (user == null)
{
return NotFound($"User with ID {id} not found");
}

user.Status = UserStatus.Approved;
await UserService.UpdateAsync(user);
return Ok($"User with ID {id} has been approved.");
}
catch (Exception ex)
{
logger.LogError($"Failed to approve user with ID {id}: " + ex.Message);
return StatusCode(500, "Internal Server Error");
}
}


[HttpPut]
public async Task<IActionResult> Update([FromBody] UserDTO user)
Expand Down
4 changes: 4 additions & 0 deletions WebApi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

string clientUrl = Env.GetString("CLIENT_URL");

// Configure DbContext
builder.Services.AddDbContext<MyDbContext>();


// Configure DbContext
builder.Services.AddDbContext<MyDbContext>();

Expand Down