Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR-12] Dev cleanup & swagger #12

Merged
merged 8 commits into from
Jan 31, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.0" />
<PackageReference Include="Simply.Track" Version="1.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="7.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using ChatService.APIs.REST.Controllers.V1.ChatMessages.Models;
using ChatService.Core.ChatMessages;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;

namespace ChatService.APIs.REST.Controllers.V1.ChatMessages;

Expand All @@ -20,6 +21,10 @@ public ChatMessagesController(ILogger<ChatMessagesController> logger, IChatMessa
}

[HttpGet("{messageId}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[SwaggerOperation(Summary = "Get Chat Message by ID", Description = "Gets a Chat Message by its ID")]
public async Task<ActionResult<GetChatMessageResponse>> Get(string messageId)
{
_logger.LogInformation("Received request to get chat message by messageId");
Expand All @@ -30,6 +35,11 @@ public async Task<ActionResult<GetChatMessageResponse>> Get(string messageId)
}

[HttpPut()]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[SwaggerOperation(Summary = "Update Chat Message", Description = "Updates a Chat Message")]
public async Task<ActionResult> Update([FromBody] UpdateChatMessageRequest request)
{
_logger.LogInformation("Received request to update chat message");
Expand All @@ -40,6 +50,11 @@ public async Task<ActionResult> Update([FromBody] UpdateChatMessageRequest reque
}

[HttpDelete()]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[SwaggerOperation(Summary = "Delete Chat Message", Description = "Deletes a Chat Message")]
public async Task<ActionResult> Delete([FromBody] DeleteChatMessageRequest request)
{
_logger.LogInformation("Received request to delete chat message");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using ChatService.Core.ChatRooms;
using ChatService.Core.ChatRooms.Commands;
using Microsoft.AspNetCore.Mvc;
using Swashbuckle.AspNetCore.Annotations;

namespace ChatService.APIs.REST.Controllers.V1.ChatRooms;

Expand All @@ -25,6 +26,10 @@ public ChatRoomsController(ILogger<ChatRoomsController> logger, IChatRoomService
}

[HttpGet("{id}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[SwaggerOperation(Summary = "Get Chatroom by ID", Description = "Gets a chatroom by its ID")]
public async Task<ActionResult<GetChatRoomResponse>> Get(string id)
{
_logger.LogInformation("Received request to get chatroom by id");
Expand All @@ -36,18 +41,27 @@ public async Task<ActionResult<GetChatRoomResponse>> Get(string id)
}

[HttpPost]
public async Task<ActionResult<CreateChatMessageResponse>> Create([FromBody] CreateChatRoomRequest request)
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[SwaggerOperation(Summary = "Create Chatroom", Description = "Creates a Chatroom")]
public async Task<ActionResult<CreateChatRoomResponse>> Create([FromBody] CreateChatRoomRequest request)
{
_logger.LogInformation("Received request to create chatroom");
var command = CreateChatRoomRequest.Convert(request);
var chatroom = await _chatRoomService.Create(command);

_logger.LogInformation("Request to create chatroom completed");
var response = CreateChatRoomResponse.Convert(chatroom, chatroom.SuperUser);
return Ok(response);
return response;
}

[HttpPut]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[SwaggerOperation(Summary = "Update Chatroom", Description = "Updates a Chatroom")]
public async Task<ActionResult> Update([FromBody] UpdateChatRoomRequest request)
{
_logger.LogInformation("Received request to update chatroom");
Expand All @@ -59,6 +73,10 @@ public async Task<ActionResult> Update([FromBody] UpdateChatRoomRequest request)
}

[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[SwaggerOperation(Summary = "Delete Chatroom", Description = "Deletes a Chatroom")]
public async Task<ActionResult> Delete(string id)
{
_logger.LogInformation("Received request to delete chatroom");
Expand All @@ -70,6 +88,10 @@ public async Task<ActionResult> Delete(string id)
}

[HttpGet("{chatroomId}/messages")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[SwaggerOperation(Summary = "Get Chat Messages by Chatroom ID", Description = "Gets Chat Messages by Chatroom ID")]
public async Task<ActionResult<GetChatMessagesByChatroomIdResponse>> GetChatMessages(string chatroomId)
{
_logger.LogInformation("Received request to get chat messages by chatroomId");
Expand All @@ -83,6 +105,10 @@ public async Task<ActionResult<GetChatMessagesByChatroomIdResponse>> GetChatMess
}

[HttpPost("{chatroomId}/messages")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
[SwaggerOperation(Summary = "Create Chat Message", Description = "Creates Chat Message")]
public async Task<ActionResult<CreateChatMessageResponse>> CreateChatMessage(string chatroomId, [FromBody] CreateChatMessageRequest request)
{
_logger.LogInformation("Received request to create chat message");
Expand Down

This file was deleted.

4 changes: 2 additions & 2 deletions src/Apps/ChatService.APIs.REST/CustomHttpExceptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace ChatService.APIs.REST;

public class CustomHttpExceptionHandler : IExceptionHandler
internal class CustomHttpExceptionHandler : IExceptionHandler
{
private readonly ILogger<CustomHttpExceptionHandler> _logger;
public CustomHttpExceptionHandler(ILogger<CustomHttpExceptionHandler> logger) => _logger = logger;
internal CustomHttpExceptionHandler(ILogger<CustomHttpExceptionHandler> logger) => _logger = logger;

public ValueTask<bool> TryHandleAsync(HttpContext context, Exception exception, CancellationToken cancellationToken)
{
Expand Down
16 changes: 8 additions & 8 deletions src/Apps/ChatService.APIs.REST/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
var builder = WebApplication.CreateBuilder(args);

builder.Services
.AddApiCors()
.AddCorsPolicy()
.AddVersioning()
.AddEndpointsApiExplorer()
.AddSwagger()
Expand All @@ -34,15 +34,15 @@

if (!app.Environment.IsProduction())
{
_ = app.UseSwagger();
_ = app.UseSwaggerUI();
_ = app
.UseSwagger()
.UseSwaggerUI();
}

_ = app.UseHttpsRedirection();

_ = app.UseCors("CorsPolicy");

_ = app.UseAuthorization();
_ = app
.UseHttpsRedirection()
.UseCors(ServiceCollectionExtensions.CORS_POLICY_NAME)
.UseAuthorization();

_ = app.MapControllers();

Expand Down
22 changes: 12 additions & 10 deletions src/Apps/ChatService.APIs.REST/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace ChatService.APIs.REST;

public static class ServiceCollectionExtensions
internal static class ServiceCollectionExtensions
{
public static IServiceCollection AddApiCors(this IServiceCollection services)
internal const string CORS_POLICY_NAME = "CorsPolicy";
internal static IServiceCollection AddCorsPolicy(this IServiceCollection services)
{
_ = services.AddCors(options =>
return services.AddCors(options =>
{
options.AddPolicy("CorsPolicy", builder =>
options.AddPolicy(CORS_POLICY_NAME, builder =>
{
_ = builder
// todo rod - include frontend url
Expand All @@ -19,10 +20,9 @@ public static IServiceCollection AddApiCors(this IServiceCollection services)
//.AllowCredentials();
});
});
return services;
}

public static IServiceCollection AddVersioning(this IServiceCollection services)
internal static IServiceCollection AddVersioning(this IServiceCollection services)
{
_ = services
.AddApiVersioning(options =>
Expand All @@ -44,10 +44,12 @@ public static IServiceCollection AddVersioning(this IServiceCollection services)
return services;
}

public static IServiceCollection AddSwagger(this IServiceCollection services)
internal static IServiceCollection AddSwagger(this IServiceCollection services)
{
_ = services
.AddSwaggerGen();
return services;
return services.
AddSwaggerGen(options =>
{
options.EnableAnnotations();
});
}
}
4 changes: 2 additions & 2 deletions src/Core/ChatMessages/ChatMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task<IReadOnlyCollection<ChatMessage>> GetByChatRoomId(string chatr
if (string.IsNullOrWhiteSpace(chatroomId))
throw new ArgumentException($"{nameof(chatroomId)} is required");

var chatroom = await _chatRoomService.Get(chatroomId) ?? throw new ResourceNotFoundException(nameof(Chatroom));
var chatroom = await _chatRoomService.Get(chatroomId) ?? throw new BadRequestException($"{nameof(Chatroom)} not found");

var chatMessages = await _chatMessageRepository.GetByChatRoomId(chatroom.Id);
if (chatMessages.Count is not 0)
Expand All @@ -54,7 +54,7 @@ public async Task<ChatMessage> Create(CreateChatMessageCommand command)
{
_logger.LogInformation("Creating chat message");

var chatroom = await _chatRoomService.Get(command.ChatroomId) ?? throw new ResourceNotFoundException(nameof(Chatroom));
var chatroom = await _chatRoomService.Get(command.ChatroomId) ?? throw new BadRequestException($"{nameof(Chatroom)} not found");

var newChatMessage = NewChatMessage.Create(command.ChatroomId, command.UserId, command.Content, command.CreatedAt, command.Type);
var chatMessage = await _chatMessageRepository.Create(newChatMessage);
Expand Down
4 changes: 1 addition & 3 deletions src/Core/ChatRooms/ChatRoomService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class ChatRoomService : IChatRoomService
private readonly ILogger<ChatRoomService> _logger;
private readonly IChatroomRepository _chatRoomRepository;

//todo later - inject user service
public ChatRoomService(ILogger<ChatRoomService> logger, IChatroomRepository chatRoomRepository)
{
_logger = logger;
Expand Down Expand Up @@ -38,8 +37,7 @@ public async Task<Chatroom> Create(CreateChatRoomCommand command)
{
_logger.LogInformation("Creating chatroom");

//todo create user using user service
var user = ChatRoomUser.CreateSuperUser(Guid.NewGuid().ToString(), command.Username);
var user = ChatRoomUser.CreateSuperUser(command.Username);

var newChatRoom = NewChatroom.Create(user);
var chatroom = await _chatRoomRepository.Create(newChatRoom);
Expand Down
6 changes: 4 additions & 2 deletions src/Core/ChatRooms/Models/ChatRoomUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ private ChatRoomUser(string id, string username, ChatRoomUserSettings settings,
IsSuperUser = isSuperUser;
}

public static ChatRoomUser Create(string id, string username, bool isSuperUser = false) => new(id, username, ChatRoomUserSettings.Create(ChatRoomColorSchemes.Light), false);
private static string GenerateId() => Guid.NewGuid().ToString();

public static ChatRoomUser CreateSuperUser(string id, string username) => Create(id, username, true);
public static ChatRoomUser Create(string username, bool isSuperUser = false) => new(GenerateId(), username, ChatRoomUserSettings.Create(ChatRoomColorSchemes.Light), false);

public static ChatRoomUser CreateSuperUser(string username) => Create(username, true);

public static ChatRoomUser Load(string id, string username, ChatRoomUserSettings settings, bool isSuperUser) => new(id, username, settings, isSuperUser);
}
2 changes: 0 additions & 2 deletions src/Core/ChatService.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
<Folder Include="Alerts\" />
<Folder Include="Events\" />
<Folder Include="Storage\" />
<Folder Include="Users\Commands\" />
<Folder Include="Users\Models\" />
</ItemGroup>

</Project>
5 changes: 0 additions & 5 deletions src/Core/Users/IUserRepository.cs

This file was deleted.

File renamed without changes.
4 changes: 0 additions & 4 deletions src/Core/Users/UserService.cs

This file was deleted.

5 changes: 0 additions & 5 deletions src/Core/Users/UserSettings.cs

This file was deleted.

3 changes: 0 additions & 3 deletions src/Infrastructure/ChatService.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
</ItemGroup>

<ItemGroup>
<Folder Include="Azure\CosmosDB\Users\" />
<Folder Include="Azure\NewFolder\" />
<Folder Include="InMemoryDb\Testing\Users\" />
<Folder Include="Slack\" />
</ItemGroup>

Expand Down
Loading