-
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/authorization' into develop
- Loading branch information
Showing
23 changed files
with
947 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using AdvancedAPI.Data.Models; | ||
using AdvancedAPI.Data.ViewModels.NewsArticle; | ||
using AutoMapper; | ||
|
||
namespace Business; | ||
|
||
/// <summary> | ||
/// Auto mapper profiles. | ||
/// </summary> | ||
public class MappingProfile : Profile | ||
{ | ||
/// <summary> | ||
/// Mapping Models against entities and opposite. | ||
/// </summary> | ||
public MappingProfile() | ||
{ | ||
CreateMap<NewsArticleRequestModel, NewsArticle>(); | ||
} | ||
} |
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,23 @@ | ||
using Business.Services; | ||
using Business.Services.Interfaces; | ||
|
||
namespace Business; | ||
|
||
/// <summary> | ||
/// Service extension used to prepare the business layer for usage. | ||
/// </summary> | ||
public static class ServiceExtension | ||
{ | ||
/// <summary> | ||
/// Registers everything business layer related. | ||
/// </summary> | ||
public static IServiceCollection AddBusinessServices(this IServiceCollection services) | ||
{ | ||
services.AddAutoMapper(typeof(MappingProfile).Assembly); | ||
|
||
services.AddScoped<IAuthenticationService, AuthenticationService>(); | ||
services.AddScoped<INewsArticleService, NewsArticleService>(); | ||
|
||
return services; | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
AdvancedAPI.Business/Services/Interfaces/INewsArticleService.cs
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.NewsArticle; | ||
|
||
namespace Business.Services.Interfaces; | ||
|
||
/// <summary> | ||
/// news article service. | ||
/// </summary> | ||
public interface INewsArticleService | ||
{ | ||
/// <summary> | ||
/// Inserts a new news article into the database. | ||
/// </summary> | ||
public Task<bool> CreateNewsArticle(NewsArticleRequestModel requestModel); | ||
|
||
/// <summary> | ||
/// Deletes the news article from the database. | ||
/// </summary> | ||
public Task<bool> DeleteNewsArticle(int id); | ||
} |
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,68 @@ | ||
using AdvancedAPI.Data.Models; | ||
using AdvancedAPI.Data.Repositories.Interfaces; | ||
using AdvancedAPI.Data.ViewModels.NewsArticle; | ||
using AutoMapper; | ||
using Business.Services.Interfaces; | ||
|
||
namespace Business.Services; | ||
|
||
/// <inheritdoc /> | ||
public class NewsArticleService : INewsArticleService | ||
{ | ||
private readonly ILogger<NewsArticleService> _logger; | ||
private readonly IMapper _mapper; | ||
private readonly INewsArticleRepository _newsArticleRepository; | ||
|
||
/// <summary> | ||
/// Constructor. | ||
/// </summary> | ||
public NewsArticleService(ILogger<NewsArticleService> logger, IMapper mapper, INewsArticleRepository newsArticleRepository) | ||
{ | ||
_logger = logger; | ||
_mapper = mapper; | ||
_newsArticleRepository = newsArticleRepository; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<bool> CreateNewsArticle(NewsArticleRequestModel requestModel) | ||
{ | ||
var mapped = _mapper.Map<NewsArticle>(requestModel); | ||
mapped.ReleaseDate = DateTime.Now; | ||
|
||
try | ||
{ | ||
await _newsArticleRepository.AddAsync(mapped); | ||
await _newsArticleRepository.SaveAsync(); | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(20, e, "Failed to insert the news article"); | ||
throw new Exception("Could not insert news article"); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<bool> DeleteNewsArticle(int id) | ||
{ | ||
try | ||
{ | ||
NewsArticle? newsArticle = await _newsArticleRepository.GetByIdAsync(id); | ||
if (newsArticle != null) | ||
{ | ||
_newsArticleRepository.Delete(newsArticle); | ||
await _newsArticleRepository.SaveAsync(); | ||
|
||
return true; | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(20, e, $"Failed to delete the news article with id: {id}"); | ||
throw new Exception($"Could not delete the news article with id: {id}"); | ||
} | ||
|
||
return false; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -18,8 +18,9 @@ public static async Task Initialize(IServiceProvider serviceProvider) | |
// Seed roles | ||
await SeedRoles(roleManager); | ||
|
||
// Seed admin user | ||
// Seed users | ||
await SeedAdminUser(userManager); | ||
await SeedUserUser(userManager); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -41,7 +42,7 @@ private static async Task SeedRoles(RoleManager<IdentityRole> roleManager) | |
} | ||
|
||
/// <summary> | ||
/// Seeding user into the database. | ||
/// Seeding admin user into the database. | ||
/// </summary> | ||
private static async Task SeedAdminUser(UserManager<IdentityUser> userManager) | ||
{ | ||
|
@@ -61,5 +62,27 @@ private static async Task SeedAdminUser(UserManager<IdentityUser> userManager) | |
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Seeding user user into the database. | ||
/// </summary> | ||
private static async Task SeedUserUser(UserManager<IdentityUser> userManager) | ||
{ | ||
IdentityUser? adminUser = await userManager.FindByEmailAsync("[email protected]"); | ||
if (adminUser == null) | ||
{ | ||
adminUser = new IdentityUser | ||
{ | ||
UserName = "[email protected]", | ||
Email = "[email protected]", | ||
}; | ||
|
||
IdentityResult? result = await userManager.CreateAsync(adminUser, "P@ssw0rd"); | ||
if (result.Succeeded) | ||
{ | ||
await userManager.AddToRoleAsync(adminUser, "User"); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.