Skip to content

Commit

Permalink
Renamed some DTOs to make them more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
palapapa committed Nov 29, 2024
1 parent 4c47c05 commit adeb164
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions backend/Sources/AutoMapperProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ internal sealed class AutoMapperProfile : Profile
{
public AutoMapperProfile()
{
CreateMap<Post, PostQueryDto>();
CreateMap<User, UserQueryDto>();
CreateMap<Tag, TagQueryDto>()
CreateMap<Post, PostQueryResponseDto>();
CreateMap<User, UserQueryResponseDto>();
CreateMap<Tag, TagQueryResponseDto>()
.ForMember(dest => dest.TagType, options => options.MapFrom(src => src.TagType.Name));
}
}
10 changes: 5 additions & 5 deletions backend/Sources/Controllers/PostsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ public class PostsController(CitizenProposalAppDbContext context, IMapper mapper
/// Quries a post by its ID.
/// </summary>
/// <param name="id">The ID of the post to query.</param>
/// <returns>A <see cref="PostQueryDto"/> instance that contains the content, tags, and author of the queried post.</returns>
/// <returns>A <see cref="PostQueryResponseDto"/> instance that contains the content, tags, and author of the queried post.</returns>
/// <response code="200">A post with the specified ID.</response>
/// <response code="400">The provided ID is not a valid integer.</response>
/// <response code="404">No post with the specified ID exists.</response>
[HttpGet("{id}")]
[ProducesResponseType(Status200OK)]
[ProducesResponseType(Status400BadRequest)]
[ProducesResponseType(Status404NotFound)]
public async Task<ActionResult<PostQueryDto>> GetPostById(int id)
public async Task<ActionResult<PostQueryResponseDto>> GetPostById(int id)
{
Post? post = await context.Posts
.Include(post => post.Tags)
Expand All @@ -47,7 +47,7 @@ public async Task<ActionResult<PostQueryDto>> GetPostById(int id)
{
return Problem($"No post with the ID {id} exists.", statusCode: Status404NotFound);
}
return mapper.Map<Post, PostQueryDto>(post);
return mapper.Map<Post, PostQueryResponseDto>(post);
}

/// <summary>
Expand All @@ -60,7 +60,7 @@ public async Task<ActionResult<PostQueryDto>> GetPostById(int id)
[HttpGet]
[ProducesResponseType(Status200OK)]
[ProducesResponseType(Status400BadRequest)]
public async Task<ActionResult<IEnumerable<PostQueryDto>>> GetPostsByParameters([FromQuery] GetPostsQueryParameters parameters)
public async Task<ActionResult<IEnumerable<PostQueryResponseDto>>> GetPostsByParameters([FromQuery] PostQueryRequestDto parameters)
{
if (!Enum.IsDefined(parameters.SortDirection) || !Enum.IsDefined(parameters.SortBy))
{
Expand All @@ -78,7 +78,7 @@ public async Task<ActionResult<IEnumerable<PostQueryDto>>> GetPostsByParameters(
(Descending, ByDate) => unsortedPosts.OrderByDescending(post => post.PostedTime).ThenByDescending(post => post.Id),
_ => throw new NotImplementedException("Impossible situation")
};
return Ok(mapper.Map<IEnumerable<Post>, IEnumerable<PostQueryDto>>(await sortedPosts.Skip(parameters.Start).Take(parameters.Range).ToListAsync()));
return Ok(mapper.Map<IEnumerable<Post>, IEnumerable<PostQueryResponseDto>>(await sortedPosts.Skip(parameters.Start).Take(parameters.Range).ToListAsync()));
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions backend/Sources/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,17 @@ private async Task AddSession(User user, RandomNumberGenerator rng)
/// Gets the info of a <see cref="User"/> using its ID.
/// </summary>
/// <param name="id">The ID of the user to query.</param>
/// <returns>A <see cref="UserQueryDto"/> containing info about the user.</returns>
/// <returns>A <see cref="UserQueryResponseDto"/> containing info about the user.</returns>
[HttpGet("{id}")]
[ProducesResponseType(Status200OK)]
[ProducesResponseType(Status404NotFound)]
public async Task<ActionResult<UserQueryDto>> GetUserById(int id)
public async Task<ActionResult<UserQueryResponseDto>> GetUserById(int id)
{
User? user = await context.Users.FirstOrDefaultAsync(user => user.Id == id);
if (user is null)
{
return Problem($"No user with ID \"{id}\" exists.", statusCode: Status404NotFound);
}
return mapper.Map<User, UserQueryDto>(user);
return mapper.Map<User, UserQueryResponseDto>(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace CitizenProposalApp;

/// <summary>
/// Used for query parameter validation purposes with <see cref="PostsController.GetPostsByParameters(GetPostsQueryParameters)"/>.
/// Used for query parameter validation purposes with <see cref="PostsController.GetPostsByParameters(PostQueryRequestDto)"/>.
/// </summary>
public record GetPostsQueryParameters
public record PostQueryRequestDto
{
/// <summary>
/// The start index of the <see cref="Post"/> to get. The default value is 0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace CitizenProposalApp;
/// <summary>
/// The DTO used as the response body of <see cref="Post"/> queries.
/// </summary>
public record PostQueryDto
public record PostQueryResponseDto
{
/// <inheritdoc cref="Post.Id"/>
public int Id { get; init; }
Expand All @@ -23,10 +23,10 @@ public record PostQueryDto
/// <summary>
/// The tags on this post.
/// </summary>
public required IList<TagQueryDto> Tags { get; init; }
public required IList<TagQueryResponseDto> Tags { get; init; }

/// <summary>
/// The user who made this post.
/// </summary>
public required UserQueryDto Author { get; init; }
public required UserQueryResponseDto Author { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace CitizenProposalApp;
/// <summary>
/// The DTO used as the response body of <see cref="Tag"/> queries.
/// </summary>
public record TagQueryDto
public record TagQueryResponseDto
{
/// <inheritdoc cref="Tag.Id"/>
public int Id { get; init; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace CitizenProposalApp;
/// <summary>
/// The DTO used as the response body of <see cref="User"/> queries.
/// </summary>
public class UserQueryDto
public class UserQueryResponseDto
{
/// <inheritdoc cref="User.Id"/>
public int Id { get; init; }
Expand Down

0 comments on commit adeb164

Please sign in to comment.