Skip to content

Commit

Permalink
Result model enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
protectedvoid21 committed Oct 12, 2024
1 parent 207d7ee commit 2390166
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
10 changes: 5 additions & 5 deletions src/BHC24.Api/Controllers/OfferController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public OfferController(BhcDbContext dbContext)
}

[HttpGet]
public async Task<Response<PaginationResponse<GetOfferResponse>>> GetOffersAsync(PaginationRequest request, CancellationToken ct)
public async Task<Result<PaginationResponse<GetOfferResponse>>> GetOffersAsync([FromQuery] PaginationRequest request, CancellationToken ct)
{
var offers = await _dbContext.Offers
.Select(o => new GetOfferResponse
Expand All @@ -33,11 +33,11 @@ public async Task<Response<PaginationResponse<GetOfferResponse>>> GetOffersAsync
Project = o.Project

Check warning on line 33 in src/BHC24.Api/Controllers/OfferController.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
}).PaginateAsync(request, ct);

return new Response<PaginationResponse<GetOfferResponse>>();
return Result.Ok(offers);
}

[HttpGet("{offerId}")]
public async Task<GetOfferResponse> GetOfferAsync([FromRoute]int offerId, CancellationToken ct)
public async Task<Result<GetOfferResponse>> GetOfferAsync([FromRoute]int offerId, CancellationToken ct)
{
var offer = await _dbContext.Offers
.Where(o => o.Id == offerId)
Expand All @@ -50,9 +50,9 @@ public async Task<GetOfferResponse> GetOfferAsync([FromRoute]int offerId, Cancel
Collaborators = o.Collaborators,
Tags = o.Tags,
Project = o.Project
}).SingleOrDefaultAsync(ct);
}).FirstOrDefaultAsync(ct);

return offer;
return Result.Ok(offer);
}

[HttpPut("{offerId}")]
Expand Down
4 changes: 2 additions & 2 deletions src/BHC24.Api/Controllers/ProjectController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public async Task<IActionResult> Add(AddProjectRequest request)
}

[HttpPost("/{projectId}/offer")]
public async Task<Models.Response> CreateOfferAsync([FromRoute]int projectId, [FromBody]CreateOfferRequest request, CancellationToken ct)
public async Task<Models.Result> CreateOfferAsync([FromRoute]int projectId, [FromBody]CreateOfferRequest request, CancellationToken ct)
{
var offer = new Offer
{
Expand All @@ -75,7 +75,7 @@ public async Task<IActionResult> Add(AddProjectRequest request)
await _dbContext.Offers.AddAsync(offer, ct);
await _dbContext.SaveChangesAsync(ct);

return new Models.Response();
return new Models.Result();
}

[HttpPut("{id}")]
Expand Down
37 changes: 20 additions & 17 deletions src/BHC24.Api/Models/Response.cs → src/BHC24.Api/Models/Result.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
namespace BHC24.Api.Models;

public class Response
public abstract class ResultBase
{
public bool IsSuccess { get; set; }
public string? Message { get; set; }
public StatusCode StatusCode { get; set; }

public static Response Ok()
public StatusCode StatusCode { get; set; }
}

public class Result : ResultBase
{
public static Result Ok()
{
return new Response
return new Result
{
IsSuccess = true,
Message = "Request successful",
StatusCode = StatusCode.Ok
};
}

public static Response<T> Ok<T>(T data) where T : class
public static Result<T> Ok<T>(T? data)
{
return new Response<T>
return new Result<T>
{
Data = data,
IsSuccess = true,
Expand All @@ -27,9 +30,9 @@ public static Response<T> Ok<T>(T data) where T : class
};
}

public static Response<T> Fail<T>(string message) where T : class
public static Result<T> Fail<T>(string message) where T : class
{
return new Response<T>
return new Result<T>
{
IsSuccess = false,
Message = message,
Expand All @@ -38,26 +41,26 @@ public static Response<T> Fail<T>(string message) where T : class
}
}

public class Response<T> : Response where T : class
public class Result<T> : ResultBase
{
public T Data { get; set; } = null!;
public T Data { get; set; } = default!;

public static Response<T> Fail(string message)
public static Result<T> Fail(string message)
{
return new Response<T>
return new Result<T>
{
Data = null!,
Data = default,

Check warning on line 52 in src/BHC24.Api/Models/Result.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
IsSuccess = false,
Message = message,
StatusCode = StatusCode.BadRequest
};
}

public static Response<T> NotFound(string entityName)
public static Result<T> NotFound(string entityName)
{
return new Response<T>
return new Result<T>
{
Data = null!,
Data = default,

Check warning on line 63 in src/BHC24.Api/Models/Result.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
IsSuccess = false,
Message = $"{entityName} was not found",
StatusCode = StatusCode.NotFound
Expand Down

0 comments on commit 2390166

Please sign in to comment.