Skip to content

Commit

Permalink
fix: treat code to accept null image
Browse files Browse the repository at this point in the history
  • Loading branch information
adolfosp committed Jan 2, 2025
1 parent 688ef15 commit ee573c8
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public void Register(TypeAdapterConfig config)
.Map(dest => dest.Location, src => new Coordinate(src.Latitude, src.Longitude))
.Map(dest => dest.Size, src => src.Size)
.Map(dest => dest.Type, src => src.Type)
.Map(dest => dest.Image, src => src.Image)
.Map(dest => dest.Cnpj, src => new Cnpj(src.Cnpj));

config.NewConfig<Farm, FarmModel>()
Expand Down
15 changes: 11 additions & 4 deletions Back-Orange-Finance/Orange-Finance/Endpoints/Farms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Microsoft.AspNetCore.Mvc;

using OrangeFinance.Application.Amqp;
using OrangeFinance.Application.Farms.Commands.CreateFarm;
using OrangeFinance.Application.Farms.Queries.Farms;
using OrangeFinance.Contracts.Farms;
Expand All @@ -19,17 +20,23 @@ public static void RegisterFarmEndpoints(this IEndpointRouteBuilder routes)

var farms = routes.MapGroup("/farms");

farms.MapPost("", async (IMediator mediator, IMapper mapper, [FromBody] CreateFarmRequest request) =>
farms.MapPost("", async (IMediator mediator, IMapper mapper, AmqpFarmService aqmpFarm, [FromBody] CreateFarmRequest request) =>
{
//TODO: Validar envio de imagem da terra, criar fila, workers, adicionar cnpj (object value)

//WIP: Validar envio de imagem da terra, criar fila, workers, adicionar cnpj (object value)
//TODO: Adicionar validação de coordenadas. FluentValidation

var command = mapper.Map<CreateFarmCommand>(request);

var result = await mediator.Send(command);

return result.Match(value => Results.Created("", value: mapper.Map<FarmResponse>(value)),
errors => errors.GetProblemsDetails());

return result.Match(value =>
{
aqmpFarm.SendLocationFarm(new { value.Location.Latitude, value.Location.Longitude });
return Results.Created("", value: mapper.Map<FarmResponse>(value));
},
errors => errors.GetProblemsDetails());

}).Produces(statusCode: 400)
.Produces(statusCode: 201)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static void RegisterMiddlewares(this WebApplication app)
app.MapScalarApiReference(options =>
{
options
.WithTitle("My custom API")
.WithTitle("Orange Finance API")
.WithTheme(ScalarTheme.Alternate)
.WithSidebar(true)
.WithDefaultHttpClient(ScalarTarget.CSharp, ScalarClient.HttpClient)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public FarmInputType()
Field(x => x.Latitude).Description("Latitude da fazenda");
Field(x => x.Size).Description("Tamanho da fazenda");
Field(x => x.Type).Description("Tipo da fazenda");
Field(x => x.Image).Description("Imagem da fazenda");

}
}
29 changes: 29 additions & 0 deletions Back-Orange-Finance/OrangeFinance.Adapters/Rpc/AmqpRpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public void Send<TRequest>(string exchangeName, string routingKey, TRequest requ
currentActivity.AddTag("RoutingKey", routingKey);
currentActivity.AddTag("CallbackQueue", callbackQueueName);

this.model.ConfirmSelect();

IBasicProperties requestProperties = this.model.CreateBasicProperties()
.SetTelemetry(currentActivity)
.SetMessageId()
Expand All @@ -37,11 +39,38 @@ public void Send<TRequest>(string exchangeName, string routingKey, TRequest requ
currentActivity.AddTag("MessageId", requestProperties.MessageId);
currentActivity.AddTag("CorrelationId", requestProperties.CorrelationId);

// Registrar eventos de confirmação e não confirmação
HandleBasicAckNack();

this.model.BasicPublish(exchangeName, routingKey, requestProperties, this.serializer.Serialize(requestProperties, requestModel));

HandleDelayConfirmation();

currentActivity.SetEndTime(DateTime.UtcNow);
}

private void HandleBasicAckNack()
{
this.model.BasicAcks += (sender, ea) =>
{
Console.WriteLine($"Mensagem com tag {ea.DeliveryTag} confirmada pelo RabbitMQ.");
};

this.model.BasicNacks += (sender, ea) =>
{
Console.WriteLine($"Mensagem com tag {ea.DeliveryTag} não confirmada pelo RabbitMQ.");
};

}

private void HandleDelayConfirmation()
{
if (!this.model.WaitForConfirms(TimeSpan.FromSeconds(5)))
Console.WriteLine("Nenhuma confirmação recebida dentro do tempo limite.");
else
Console.WriteLine("Todas as mensagens foram confirmadas.");
}

protected virtual TResponse Receive<TResponse>(QueueDeclareOk queue, TimeSpan receiveTimeout)
{
using BlockingCollection<AmqpResponse<TResponse>> localQueue = [];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using OrangeFinance.Adapters.Rpc;

namespace OrangeFinance.Application.Amqp;

public sealed class AmqpFarmService(AmqpRpc amqpRpc) : AmqpServiceBase
{
private readonly AmqpRpc _amqpRpc = amqpRpc;

protected override string ExchangeName => "farm_service";

protected override string ServiceName => "FarmService";

public void SendLocationFarm<TRequest>(TRequest requestModel, string routeName = "")
{
_amqpRpc.Send(ExchangeName, routeName, requestModel);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static IServiceCollection AddApplication(this IServiceCollection services
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());

services.AddScoped<SecurityService>();
services.AddScoped<AmqpUserService>();
services.AddScoped<AmqpFarmService>();

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@

namespace OrangeFinance.Application.Farms.Commands.CreateFarm;

public record CreateFarmCommand(string Name, string Description, Coordinate Location, string Size, string Type, string Image, Cnpj Cnpj) : IRequest<ErrorOr<Farm>>;
public record CreateFarmCommand(string Name, string Description, Coordinate Location, string Size, string Type, Cnpj Cnpj) : IRequest<ErrorOr<Farm>>;
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public async Task<ErrorOr<Farm>> Handle(CreateFarmCommand request, CancellationT
latitude: request.Location.Latitude,
size: request.Size,
type: request.Type,
image: request.Image,
cnpj: request.Cnpj);

FarmModel? farmModel = _mapper.Map<FarmModel>(farm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public CreateFarmCommandValidator()
RuleFor(x => x.Location).NotNull();
RuleFor(x => x.Size).NotEmpty();
RuleFor(x => x.Type).NotEmpty();
RuleFor(x => x.Image).NotEmpty();

}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using Microsoft.AspNetCore.Http;

namespace OrangeFinance.Contracts.Farms;
namespace OrangeFinance.Contracts.Farms;

public record CreateFarmRequest(string Name,
string Description,
double Longitude,
double Latitude,
string Size,
string Type,
IFormFile Image,
string Cnpj);

7 changes: 3 additions & 4 deletions Back-Orange-Finance/OrangeFinance.Domain/Farms/Farm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ namespace OrangeFinance.Domain.Farms;

public sealed class Farm : AggregateRoot<FarmId, Guid>
{
private Farm(FarmId id, string name, string description, double longitude, double latitude, string size, string type, string image, Cnpj cnpj)
private Farm(FarmId id, string name, string description, double longitude, double latitude, string size, string type, Cnpj cnpj)

Check warning on line 10 in Back-Orange-Finance/OrangeFinance.Domain/Farms/Farm.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Image' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
{
Id = id;
Name = name;
Description = description;
Location = new Coordinate(latitude: latitude, longitude: longitude);
Size = size;
Type = type;
Image = image;
Cnpj = cnpj;
}

Expand All @@ -27,9 +26,9 @@ private Farm(FarmId id, string name, string description, double longitude, doubl
public string Image { get; private set; }
public Cnpj Cnpj { get; private set; }

public static Farm Create(string name, string description, double longitude, double latitude, string size, string type, string image, Cnpj cnpj)
public static Farm Create(string name, string description, double longitude, double latitude, string size, string type, Cnpj cnpj)
{
var farm = new Farm(id: FarmId.CreateUnique(), name: name, description: description, longitude: longitude, latitude: latitude, size: size, type: type, image: image, cnpj: cnpj);
var farm = new Farm(id: FarmId.CreateUnique(), name: name, description: description, longitude: longitude, latitude: latitude, size: size, type: type, cnpj: cnpj);
farm.AddDomainEvent(new FarmCreated(farm.Id.Value, farm.Name, farm.Description, farm.Location.Longitude, farm.Location.Latitude, farm.Size, farm.Type, farm.Image));
return farm;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace OrangeFinance.Domain.Farms.Models;

public record class FarmModel(Guid Id, string Name, string Description, double Longitude, double Latitude, string Size, string Type, string Image, string Cnpj);
public record class FarmModel(Guid Id, string Name, string Description, double Longitude, double Latitude, string Size, string Type, string? Image, string Cnpj);
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public void Configure(EntityTypeBuilder<FarmModel> builder)
.Property(m => m.Type);

builder
.Property(m => m.Image);
.Property(m => m.Image)
.IsRequired(false);
}
}

0 comments on commit ee573c8

Please sign in to comment.