Skip to content

Commit

Permalink
Merge pull request #45 from Paxol/develop
Browse files Browse the repository at this point in the history
Boolean operators and TryGet method
  • Loading branch information
marcominerva committed Feb 20, 2024
2 parents d86190f + 76522b8 commit 9c0fa86
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 1 deletion.
Expand Up @@ -34,6 +34,18 @@ public async Task<IActionResult> GetPerson(Guid id)
return response;
}

[HttpGet("{id:guid}/with-image", Name = nameof(GetPersonWithImage))]
[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(PersonWithImage))]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetPersonWithImage(Guid id)
{
// You can collapse the following instructions into a single one.
var result = await peopleService.GetWithImageAsync(id);

var response = HttpContext.CreateResponse(result); // Or result.ToResponse(HttpContext)
return response;
}

[HttpPost]
[ProducesResponseType<Person>(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
Expand Down
13 changes: 13 additions & 0 deletions samples/MinimalApis/OperationResults.Sample/Program.cs
Expand Up @@ -74,6 +74,19 @@
.WithName("GetPerson")
.WithOpenApi();

peopleApi.MapGet("{id:guid}/with-image", async (Guid id, IPeopleService peopleService, HttpContext httpContext) =>
{
// You can collapse the following instructions into a single one.
var result = await peopleService.GetWithImageAsync(id);
var response = httpContext.CreateResponse(result); // Or result.ToResponse(httpContext)
return response;
})
.Produces<PersonWithImage>()
.Produces(StatusCodes.Status404NotFound)
.WithName("GetPersonWithImage")
.WithOpenApi();

peopleApi.MapPost("/", async (Person person, IPeopleService peopleService, HttpContext httpContext) =>
{
// You can collapse the following instructions into a single one.
Expand Down
Expand Up @@ -8,6 +8,8 @@ public interface IPeopleService

Task<Result<Person>> GetAsync(Guid id);

Task<Result<PersonWithImage>> GetWithImageAsync(Guid id);

Task<Result<Person>> SaveAsync(Person person);

Task<Result> DeleteAsync(Guid id);
Expand Down
Expand Up @@ -6,7 +6,7 @@

namespace OperationResults.Sample.BusinessLayer.Services;

public class PeopleService(ApplicationDbContext dbContext) : IPeopleService
public class PeopleService(ApplicationDbContext dbContext, IImageService imageService) : IPeopleService
{
public async Task<Result<IEnumerable<Person>>> GetAsync()
{
Expand Down Expand Up @@ -45,6 +45,38 @@ public async Task<Result<Person>> GetAsync(Guid id)
return person;
}

public async Task<Result<PersonWithImage>> GetWithImageAsync(Guid id)
{
var personResult = await GetAsync(id);
if (!personResult) // A shortcut to !personResult.Success
{
// The person operation failed
return Result.Fail(personResult.FailureReason);
}

var person = personResult.Content!;
var imageResult = await imageService.GetImageAsync();
if (imageResult.TryGet(out var imageFileContent) && imageFileContent is not null)
{
// The image operation succeeded, return person with image
var personWithImage = new PersonWithImage
{
Person = person,
Image = imageFileContent.Content
};

return personWithImage;
}

// The image operation failed, return person without image
var personWithoutImage = new PersonWithImage
{
Person = person
};

return personWithoutImage;
}

public async Task<Result<Person>> SaveAsync(Person person)
{
var dbPerson = person.Id != Guid.Empty ? await dbContext.People
Expand Down
@@ -0,0 +1,7 @@
namespace OperationResults.Sample.Shared.Models;

public class PersonWithImage
{
public Person Person { get; set; }
public byte[] Image { get; set; }
}
2 changes: 2 additions & 0 deletions src/OperationResults/IGenericResult{OfT}.cs
Expand Up @@ -3,4 +3,6 @@ namespace OperationResults;
public interface IGenericResult<T> : IGenericResult
{
public T? Content { get; }

public bool TryGet(out T? value);
}
9 changes: 9 additions & 0 deletions src/OperationResults/Result.cs
Expand Up @@ -52,4 +52,13 @@ public static Result Fail(int failureReason, string message, string detail, IEnu

public static Result Fail(int failureReason, Exception? error, IEnumerable<ValidationError>? validationErrors = null)
=> new(false, failureReason: failureReason, error: error, validationErrors: validationErrors);

public static bool operator true(Result result)
=> result.Success;

public static bool operator false(Result result)
=> !result.Success;

public static implicit operator bool(Result result)
=> result.Success;
}
15 changes: 15 additions & 0 deletions src/OperationResults/Result{OfT}.cs
Expand Up @@ -29,6 +29,12 @@ internal Result(bool success = true, T? content = default, int failureReason = F
ValidationErrors = validationErrors;
}

public bool TryGet(out T? value)
{
value = Content;
return Success;
}

public static Result<T> Ok(T? content = default)
=> new(success: true, content);

Expand Down Expand Up @@ -67,4 +73,13 @@ public static Result<T> Fail(int failureReason, Exception? error, IEnumerable<Va

public static implicit operator Result<T>(Result result)
=> new(result.Success, default, result.FailureReason, result.ErrorMessage, result.ErrorDetail, result.Error, result.ValidationErrors);

public static bool operator true(Result<T> result)
=> result.Success;

public static bool operator false(Result<T> result)
=> !result.Success;

public static implicit operator bool(Result<T> result)
=> result.Success;
}

0 comments on commit 9c0fa86

Please sign in to comment.