Skip to content

Commit 84c0efb

Browse files
authored
Merge pull request #29 from kubagdynia/develop
Code refactoring
2 parents 94e9787 + 5f9df2e commit 84c0efb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+919
-968
lines changed

DapperMappers/DapperMappers.Api/Contracts/Core/BadRequestMessage.cs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,25 @@
33
using FluentValidation.Results;
44
using Microsoft.AspNetCore.Http;
55

6-
namespace DapperMappers.Api.Contracts.Core
6+
namespace DapperMappers.Api.Contracts.Core;
7+
8+
public record BadRequestMessage : BaseResponse
79
{
8-
public class BadRequestMessage : BaseResponse
10+
public BadRequestMessage()
911
{
10-
public BadRequestMessage()
11-
{
12-
StatusCode = StatusCodes.Status400BadRequest;
13-
}
12+
StatusCode = StatusCodes.Status400BadRequest;
13+
}
1414

15-
public BadRequestMessage(IList<ValidationFailure>? errors) : this()
15+
public BadRequestMessage(IList<ValidationFailure>? errors) : this()
16+
{
17+
if (errors == null || !errors.Any()) return;
18+
foreach (var error in errors)
1619
{
17-
if (errors == null || !errors.Any()) return;
18-
foreach (var error in errors)
19-
{
20-
AddError(
21-
code: error.ErrorCode,
22-
message: "Validation failed",
23-
userMessage: error.ErrorMessage,
24-
details: $"Validation failed for '{error.PropertyName}' with value '{error.AttemptedValue}'");
25-
}
20+
AddError(
21+
code: error.ErrorCode,
22+
message: "Validation failed",
23+
userMessage: error.ErrorMessage,
24+
details: $"Validation failed for '{error.PropertyName}' with value '{error.AttemptedValue}'");
2625
}
2726
}
28-
}
27+
}

DapperMappers/DapperMappers.Api/Contracts/Core/BaseResponse.cs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,22 @@
22
using System.Text.Json;
33
using DapperMappers.Api.Serializers;
44

5-
namespace DapperMappers.Api.Contracts.Core
6-
{
7-
public abstract class BaseResponse
8-
{
9-
public int StatusCode { get; set; }
10-
11-
public IList<Error>? Errors { get; private set; }
12-
13-
protected void AddError(string code, string message, string? details = null, string? userMessage = null)
14-
{
15-
Errors ??= new List<Error>();
5+
namespace DapperMappers.Api.Contracts.Core;
166

17-
Errors.Add(new Error
18-
{
19-
Code = code,
20-
Message = message,
21-
Details = details,
22-
UserMessage = userMessage
23-
});
24-
}
7+
public abstract record BaseResponse
8+
{
9+
public int StatusCode { get; set; }
2510

26-
public override string ToString()
27-
=> JsonSerializer.Serialize(this, BaseJsonOptions.GetJsonSerializerOptions);
28-
}
11+
public IList<Error>? Errors { get; private set; }
2912

30-
public class Error
13+
protected void AddError(string code, string message, string? details = null, string? userMessage = null)
3114
{
32-
public string? Message { get; set; }
33-
public string? Code { get; set; }
34-
public string? Details { get; set; }
35-
public string? UserMessage { get; set; }
15+
Errors ??= new List<Error>();
16+
Errors.Add(new Error(Message: message, Code: code, Details: details, UserMessage: userMessage));
3617
}
18+
19+
public override string ToString()
20+
=> JsonSerializer.Serialize(this, BaseJsonOptions.GetJsonSerializerOptions);
3721
}
22+
23+
public record Error(string? Message, string? Code, string? Details, string? UserMessage);
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using Microsoft.AspNetCore.Http;
22

3-
namespace DapperMappers.Api.Contracts.Core
3+
namespace DapperMappers.Api.Contracts.Core;
4+
5+
public record NotFoundMessage : BaseResponse
46
{
5-
public class NotFoundMessage : BaseResponse
7+
public NotFoundMessage(string message)
68
{
7-
public NotFoundMessage(string message)
8-
{
9-
StatusCode = StatusCodes.Status404NotFound;
10-
AddError(StatusCodes.Status404NotFound.ToString(), message);
11-
}
9+
StatusCode = StatusCodes.Status404NotFound;
10+
AddError(StatusCodes.Status404NotFound.ToString(), message);
1211
}
13-
}
12+
}
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
namespace DapperMappers.Api.Contracts.Core
1+
namespace DapperMappers.Api.Contracts.Core;
2+
3+
public abstract record Response<T> : BaseResponse
24
{
3-
public abstract class Response<T> : BaseResponse
4-
{
5-
public T Result { get; set; }
5+
public T Result { get; set; }
66

7-
protected Response(T result, int statusCode)
8-
{
9-
Result = result;
10-
StatusCode = statusCode;
11-
}
7+
protected Response(T result, int statusCode)
8+
{
9+
Result = result;
10+
StatusCode = statusCode;
1211
}
13-
}
12+
}
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
using DapperMappers.Api.Contracts.V1.Resources;
22
using System;
33

4-
namespace DapperMappers.Api.Contracts.V1.Requests
4+
namespace DapperMappers.Api.Contracts.V1.Requests;
5+
6+
public record AddBookRequest
57
{
6-
public record AddBookRequest
7-
{
8-
public string Title { get; set; } = string.Empty;
8+
public string Title { get; set; } = string.Empty;
99

10-
public int PageCount { get; set; }
10+
public int PageCount { get; set; }
1111

12-
public string Isbn { get; set; } = string.Empty;
12+
public string Isbn { get; set; } = string.Empty;
1313

14-
public DateTime DateOfPublication { get; set; }
14+
public DateTime DateOfPublication { get; set; }
1515

16-
public BookAuthorsResource Authors { get; set; } = new();
16+
public BookAuthorsResource Authors { get; set; } = new();
1717

18-
public BookTableOfContentsResource TableOfContents { get; set; } = new();
18+
public BookTableOfContentsResource TableOfContents { get; set; } = new();
1919

20-
public string ShortDescription { get; set; } = string.Empty;
20+
public string ShortDescription { get; set; } = string.Empty;
2121

22-
public BookDescriptionResource Description { get; set; } = new();
22+
public BookDescriptionResource Description { get; set; } = new();
2323

24-
public string Publisher { get; set; } = string.Empty;
24+
public string Publisher { get; set; } = string.Empty;
2525

26-
public string Url { get; set; } = string.Empty;
27-
}
28-
}
26+
public string Url { get; set; } = string.Empty;
27+
}
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
using Microsoft.AspNetCore.Mvc;
22
using System;
33

4-
namespace DapperMappers.Api.Contracts.V1.Requests
4+
namespace DapperMappers.Api.Contracts.V1.Requests;
5+
6+
public record DeleteBookRequest
57
{
6-
public record DeleteBookRequest
7-
{
8-
/// <summary>
9-
/// Book id
10-
/// </summary>
11-
[FromRoute(Name = "id")]
12-
public Guid Id { get; set; }
13-
}
14-
}
8+
/// <summary>
9+
/// Book id
10+
/// </summary>
11+
[FromRoute(Name = "id")]
12+
public Guid Id { get; set; }
13+
}
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
using Microsoft.AspNetCore.Mvc;
22
using System;
33

4-
namespace DapperMappers.Api.Contracts.V1.Requests
4+
namespace DapperMappers.Api.Contracts.V1.Requests;
5+
6+
public record GetBookRequest
57
{
6-
public record GetBookRequest
7-
{
8-
/// <summary>
9-
/// Book id
10-
/// </summary>
11-
[FromRoute(Name = "id")]
12-
public Guid Id { get; set; }
13-
}
14-
}
8+
/// <summary>
9+
/// Book id
10+
/// </summary>
11+
[FromRoute(Name = "id")]
12+
public Guid Id { get; set; }
13+
}
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
using System.Collections.Generic;
22

3-
namespace DapperMappers.Api.Contracts.V1.Resources
3+
namespace DapperMappers.Api.Contracts.V1.Resources;
4+
5+
public record BookAuthorsResource
46
{
5-
public record BookAuthorsResource
6-
{
7-
public List<AuthorResource> Authors { get; set; }
8-
}
7+
public List<AuthorResource> Authors { get; set; }
8+
}
99

10-
public record AuthorResource
11-
{
12-
public string Name { get; set; }
10+
public record AuthorResource
11+
{
12+
public string Name { get; set; }
1313

14-
public string Description { get; set; }
15-
}
16-
}
14+
public string Description { get; set; }
15+
}
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
using System.Collections.Generic;
22

3-
namespace DapperMappers.Api.Contracts.V1.Resources
4-
{
5-
public record BookDescriptionResource
6-
{
7-
public LearnResource Learn { get; set; }
3+
namespace DapperMappers.Api.Contracts.V1.Resources;
84

9-
public string About { get; set; }
5+
public record BookDescriptionResource
6+
{
7+
public LearnResource Learn { get; set; }
108

11-
public FeaturesResource Features { get; set; }
12-
}
9+
public string About { get; set; }
1310

14-
public record LearnResource
15-
{
16-
public List<string> Points { get; set; }
17-
}
11+
public FeaturesResource Features { get; set; }
12+
}
1813

19-
public record FeaturesResource
20-
{
21-
public List<string> Points { get; set; }
22-
}
14+
public record LearnResource
15+
{
16+
public List<string> Points { get; set; }
2317
}
18+
19+
public record FeaturesResource
20+
{
21+
public List<string> Points { get; set; }
22+
}
Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
using System;
22

3-
namespace DapperMappers.Api.Contracts.V1.Resources
3+
namespace DapperMappers.Api.Contracts.V1.Resources;
4+
5+
public record BookResource
46
{
5-
public record BookResource
6-
{
7-
public string Id { get; set; }
7+
public string Id { get; set; }
88

9-
public string Title { get; set; }
9+
public string Title { get; set; }
1010

11-
public int PageCount { get; set; }
11+
public int PageCount { get; set; }
1212

13-
public string Isbn { get; set; }
13+
public string Isbn { get; set; }
1414

15-
public DateTime DateOfPublication { get; set; }
15+
public DateTime DateOfPublication { get; set; }
1616

17-
public BookAuthorsResource Authors { get; set; }
17+
public BookAuthorsResource Authors { get; set; }
1818

19-
public BookTableOfContentsResource TableOfContents { get; set; }
19+
public BookTableOfContentsResource TableOfContents { get; set; }
2020

21-
public string ShortDescription { get; set; }
21+
public string ShortDescription { get; set; }
2222

23-
public BookDescriptionResource Description { get; set; }
23+
public BookDescriptionResource Description { get; set; }
2424

25-
public string Publisher { get; set; }
25+
public string Publisher { get; set; }
2626

27-
public string Url { get; set; }
28-
}
29-
}
27+
public string Url { get; set; }
28+
}

0 commit comments

Comments
 (0)