-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/user-related-endpoints' into develop
- Loading branch information
Showing
41 changed files
with
1,706 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using AdvancedAPI.Data.ViewModels.User; | ||
|
||
namespace Business.Services.Interfaces; | ||
|
||
/// <summary> | ||
/// User service. | ||
/// </summary> | ||
public interface IUserService | ||
{ | ||
/// <summary> | ||
/// Gets the user profile of the given user id. | ||
/// </summary> | ||
public Task<UserProfileResponseModel?> GetUserProfile(string userId); | ||
|
||
/// <summary> | ||
/// Updates the last seen state of the user. | ||
/// </summary> | ||
public Task UpdateLastSeen(string userId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System.Globalization; | ||
using AdvancedAPI.Data.Models; | ||
using AdvancedAPI.Data.Repositories.Interfaces; | ||
using AdvancedAPI.Data.ViewModels.User; | ||
using AutoMapper; | ||
using Business.Services.Interfaces; | ||
|
||
namespace Business.Services; | ||
|
||
/// <inheritdoc /> | ||
public class UserService : IUserService | ||
{ | ||
private readonly IIdentityRepository _identityRepository; | ||
private readonly IGenderRepository _genderRepository; | ||
private readonly ILastSeenRepository _lastSeenRepository; | ||
private readonly IMapper _mapper; | ||
|
||
/// <summary> | ||
/// Constructor. | ||
/// </summary> | ||
public UserService( | ||
IMapper mapper, | ||
IIdentityRepository identityRepository, | ||
IGenderRepository genderRepository, | ||
ILastSeenRepository lastSeenRepository) | ||
{ | ||
_identityRepository = identityRepository; | ||
_genderRepository = genderRepository; | ||
_lastSeenRepository = lastSeenRepository; | ||
_mapper = mapper; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<UserProfileResponseModel?> GetUserProfile(string userId) | ||
{ | ||
User? user = await _identityRepository.GetUserById(userId); | ||
|
||
if (user == null) | ||
{ | ||
return null; | ||
} | ||
|
||
user.Gender = await _genderRepository.GetByIdAsync(user.GenderId); | ||
|
||
UserProfileResponseModel responseModel = _mapper.Map<UserProfileResponseModel>(user); | ||
|
||
responseModel.Birthday = user.DateOfBirth.ToString("MMMM d"); | ||
LastSeen? lastSeen = await _lastSeenRepository.GetByUserId(user.Id); | ||
|
||
responseModel.LastSeen = lastSeen != null ? lastSeen.DateTime.ToString(CultureInfo.InvariantCulture) : "unknown"; | ||
return responseModel; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task UpdateLastSeen(string userId) | ||
{ | ||
LastSeen? lastSeen = await _lastSeenRepository.GetByUserId(userId); | ||
|
||
if (lastSeen != null) | ||
{ | ||
lastSeen.DateTime = DateTime.Now; | ||
_lastSeenRepository.Update(lastSeen); | ||
} | ||
else | ||
{ | ||
await _lastSeenRepository.AddAsync( | ||
new LastSeen() | ||
{ | ||
UserId = userId, | ||
DateTime = DateTime.Now, | ||
}); | ||
} | ||
|
||
await _lastSeenRepository.SaveAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using AdvancedAPI.Data.Repositories; | ||
using AdvancedAPI.Data.Repositories.Interfaces; | ||
|
||
namespace AdvancedAPI.Data; | ||
|
||
/// <summary> | ||
/// Service extension used to prepare the data layer for usage. | ||
/// </summary> | ||
public static class DataExtension | ||
{ | ||
/// <summary> | ||
/// Registers everything data layer related. | ||
/// </summary> | ||
public static void AddDataRepositories(this IServiceCollection services) | ||
{ | ||
services.AddScoped<IIdentityRepository, IdentityRepository>(); | ||
services.AddScoped<INewsArticleRepository, NewsArticleRepository>(); | ||
services.AddScoped<IGenderRepository, GenderRepository>(); | ||
services.AddScoped<ILastSeenRepository, LastSeenRepository>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using Microsoft.AspNetCore.Identity; | ||
using AdvancedAPI.Data.Models; | ||
using Microsoft.AspNetCore.Identity; | ||
|
||
namespace AdvancedAPI.Data | ||
{ | ||
|
@@ -12,7 +13,7 @@ public class DbInitializer | |
/// </summary> | ||
public static async Task Initialize(IServiceProvider serviceProvider) | ||
{ | ||
UserManager<IdentityUser> userManager = serviceProvider.GetRequiredService<UserManager<IdentityUser>>(); | ||
UserManager<User> userManager = serviceProvider.GetRequiredService<UserManager<User>>(); | ||
RoleManager<IdentityRole> roleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>(); | ||
|
||
// Seed roles | ||
|
@@ -44,15 +45,18 @@ private static async Task SeedRoles(RoleManager<IdentityRole> roleManager) | |
/// <summary> | ||
/// Seeding admin user into the database. | ||
/// </summary> | ||
private static async Task SeedAdminUser(UserManager<IdentityUser> userManager) | ||
private static async Task SeedAdminUser(UserManager<User> userManager) | ||
{ | ||
IdentityUser? adminUser = await userManager.FindByEmailAsync("[email protected]"); | ||
User? adminUser = await userManager.FindByEmailAsync("[email protected]"); | ||
if (adminUser == null) | ||
{ | ||
adminUser = new IdentityUser | ||
adminUser = new User | ||
{ | ||
UserName = "[email protected]", | ||
Email = "[email protected]", | ||
DateOfBirth = new DateTime(1980, 1, 1), | ||
DisplayName = "Admin User", | ||
GenderId = 1 // Assuming 1 is the ID for Male | ||
}; | ||
|
||
IdentityResult? result = await userManager.CreateAsync(adminUser, "P@ssw0rd"); | ||
|
@@ -66,21 +70,24 @@ private static async Task SeedAdminUser(UserManager<IdentityUser> userManager) | |
/// <summary> | ||
/// Seeding user user into the database. | ||
/// </summary> | ||
private static async Task SeedUserUser(UserManager<IdentityUser> userManager) | ||
private static async Task SeedUserUser(UserManager<User> userManager) | ||
{ | ||
IdentityUser? adminUser = await userManager.FindByEmailAsync("[email protected]"); | ||
if (adminUser == null) | ||
User? userUser = await userManager.FindByEmailAsync("[email protected]"); | ||
if (userUser == null) | ||
{ | ||
adminUser = new IdentityUser | ||
userUser = new User | ||
{ | ||
UserName = "[email protected]", | ||
Email = "[email protected]", | ||
DateOfBirth = new DateTime(1990, 1, 1), | ||
DisplayName = "Regular User", | ||
GenderId = 2 // Assuming 2 is the ID for Female | ||
}; | ||
|
||
IdentityResult? result = await userManager.CreateAsync(adminUser, "P@ssw0rd"); | ||
IdentityResult? result = await userManager.CreateAsync(userUser, "P@ssw0rd"); | ||
if (result.Succeeded) | ||
{ | ||
await userManager.AddToRoleAsync(adminUser, "User"); | ||
await userManager.AddToRoleAsync(userUser, "User"); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.