Skip to content

Commit

Permalink
news article list of non-logged in is added + routes modified a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
DustSwiffer committed Jul 23, 2024
1 parent ced1bf5 commit 9f58c41
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 34 deletions.
1 change: 1 addition & 0 deletions AdvancedAPI.Business/MappingProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class MappingProfile : Profile
public MappingProfile()
{
CreateMap<NewsArticleRequestModel, NewsArticle>();
CreateMap<NewsArticle, NewsArticleResponseModel>();
CreateMap<User, UserProfileResponseModel>(MemberList.None)
.ForMember(d => d.Gender, opt => opt.MapFrom(s => s.Gender.Name));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface INewsArticleService
/// Deletes the news article from the database.
/// </summary>
public Task<bool> DeleteNewsArticle(int id);

public Task<List<NewsArticleResponseModel>> GetList();
}
12 changes: 12 additions & 0 deletions AdvancedAPI.Business/Services/NewsArticleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,16 @@ public async Task<bool> DeleteNewsArticle(int id)

return false;
}

/// <summary>
/// Getting list of all news articles.
/// </summary>
public async Task<List<NewsArticleResponseModel>> GetList()
{
IEnumerable<NewsArticle> newsArticles = await _newsArticleRepository.GetAllAsync();

List<NewsArticleResponseModel> mapped = _mapper.Map<IEnumerable<NewsArticleResponseModel>>(newsArticles).ToList();

return mapped;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// <copyright file="NewsArticleResponseModel.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

namespace AdvancedAPI.Data.ViewModels.NewsArticle;

/// <summary>
/// Response model for news article.
/// </summary>
public class NewsArticleResponseModel
{
/// <summary>
/// identifier.
/// </summary>
public int Id { get; set; }

/// <summary>
/// Header text.
/// </summary>
public string HeaderText { get; set; }

Check warning on line 20 in AdvancedAPI.Data/ViewModels/NewsArticle/NewsArticleResponseModel.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Non-nullable property 'HeaderText' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

/// <summary>
/// Content in HTML format.
/// </summary>
public string ContentHtml { get; set; }

Check warning on line 25 in AdvancedAPI.Data/ViewModels/NewsArticle/NewsArticleResponseModel.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Non-nullable property 'ContentHtml' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

/// <summary>
/// Release date of the article.
/// </summary>
public DateTime ReleaseDate { get; set; }
}
2 changes: 1 addition & 1 deletion AdvancedAPI/BaseControllers/AdminBaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace AdvancedAPI.BaseControllers;
/// Controller for admin related endpoints.
/// </summary>
[Authorize(Policy = "AdminPolicy")]
[Route("admin")]
[Route("api/admin/[controller]")]
public class AdminBaseController : BaseController
{
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion AdvancedAPI/BaseControllers/BaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace AdvancedAPI.BaseControllers;
/// Base controller.
/// </summary>
[ApiController]
[Route("api")]
[Route("api/[controller]")]
public class BaseController : ControllerBase
{
/// <summary>
Expand Down
25 changes: 0 additions & 25 deletions AdvancedAPI/Controllers/Admin/DashboardController.cs

This file was deleted.

1 change: 0 additions & 1 deletion AdvancedAPI/Controllers/Admin/NewsArticleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace AdvancedAPI.Controllers.Admin;
/// <summary>
/// News article admin controller.
/// </summary>
[Route("news")]
public class NewsArticleController : AdminBaseController
{
private readonly INewsArticleService _newsArticleService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ namespace AdvancedAPI.Controllers;
/// <summary>
/// Authentication endpoints.
/// </summary>
[Microsoft.AspNetCore.Components.Route("authentication")]
public class AuthenticationContoller : BaseController
public class AuthenticationController : BaseController
{
private readonly IAuthenticationService _authenticationService;

/// <summary>
/// Constructor.
/// </summary>
public AuthenticationContoller(IAuthenticationService authenticationService)
public AuthenticationController(IAuthenticationService authenticationService)
{
_authenticationService = authenticationService;
}
Expand Down
32 changes: 32 additions & 0 deletions AdvancedAPI/Controllers/NewsArticleController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using AdvancedAPI.BaseControllers;
using AdvancedAPI.Data.ViewModels.NewsArticle;
using Business.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace AdvancedAPI.Controllers;

/// <summary>
/// News article endpoint without authorization.
/// </summary>
public class NewsArticleController : BaseController
{
private readonly INewsArticleService _newsArticleService;

/// <summary>
/// Constructor.
/// </summary>
public NewsArticleController(INewsArticleService newsArticleService)
{
_newsArticleService = newsArticleService;
}

/// <summary>
/// Get list of news articles.
/// </summary>
[HttpGet]
public async Task<IActionResult> GetList()
{
List<NewsArticleResponseModel> newsArticles = await _newsArticleService.GetList();
return Ok(newsArticles);
}
}
1 change: 0 additions & 1 deletion AdvancedAPI/Controllers/User/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace AdvancedAPI.Controllers.User;
/// <summary>
/// User endpoints
/// </summary>
[Route("user")]
public class UserController : UserBaseController
{
private readonly IUserService _userService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Business.Services.Interfaces;
using Microsoft.AspNetCore.Mvc.Filters;

namespace AdvancedAPI.ActionFilters;
namespace AdvancedAPI.Filters;

/// <summary>
/// Last seen action filter.
Expand Down
27 changes: 27 additions & 0 deletions AdvancedAPI/Filters/LowercaseDocumentFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerGen;

namespace AdvancedAPI.Filters;

/// <summary>
/// Lower case filteer will convert the url paths to lowercase.
/// </summary>
public class LowercaseDocumentFilter : IDocumentFilter
{
/// <summary>
/// Applies the lowercase convertion.
/// </summary>
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
{
Dictionary<string, OpenApiPathItem> paths = swaggerDoc.Paths.ToDictionary(
path => path.Key.ToLowerInvariant(),
path => path.Value);

swaggerDoc.Paths.Clear();

foreach (KeyValuePair<string, OpenApiPathItem> path in paths)
{
swaggerDoc.Paths.Add(path.Key, path.Value);
}
}
}
3 changes: 2 additions & 1 deletion AdvancedAPI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Reflection;
using System.Text;
using AdvancedAPI.ActionFilters;
using AdvancedAPI.Data;
using AdvancedAPI.Data.Models;
using AdvancedAPI.Filters;
using Business;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
Expand All @@ -28,6 +28,7 @@
builder.Services.AddSwaggerGen(
c =>
{
c.DocumentFilter<LowercaseDocumentFilter>();
c.SwaggerDoc(
"v1",
new OpenApiInfo
Expand Down

0 comments on commit 9f58c41

Please sign in to comment.