Skip to content

Commit

Permalink
straigth to prod
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew-Beslogic committed Oct 17, 2024
1 parent 3ebdf88 commit 96cf265
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
8 changes: 6 additions & 2 deletions apps/releaf/backend/Releaf.Api/Controllers/BoxesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Releaf.Application.Commands;
using Releaf.Application.Query;
using Releaf.Auth;
using Releaf.Domain.Boxes;
using Releaf.Domain.Repo;
Expand Down Expand Up @@ -41,9 +42,12 @@ public async Task<InitializeBoxCmdResult> Initialize(string treeDefinitionId, st
}

[HttpGet]
public IEnumerable<BoxAggregate> GetAll()
public async Task<IEnumerable<BoxAggregate>> GetAll()
{
return boxRepo.GetBoxesForUser(currentUser.Id);
var cmd = new GetBoxesQuery(currentUser.Id);
var result = await mediator.Send(cmd);

return result;
}

[HttpGet("{id}")]
Expand Down
41 changes: 41 additions & 0 deletions apps/releaf/backend/Releaf.Application/Query/GetBoxesQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using MediatR;
using Releaf.Domain.Boxes;
using Releaf.Domain.Repo;
using Releaf.Shared;

namespace Releaf.Application.Query;

public class GetBoxesQuery : IRequest<IEnumerable<BoxAggregate>>
{
public GetBoxesQuery(UserId ownerId)
{
OwnerId = ownerId;
}

public UserId OwnerId { get; }
}

public class GetBoxesQueryHandler : IRequestHandler<GetBoxesQuery, IEnumerable<BoxAggregate>>
{
public GetBoxesQueryHandler(IBoxRepo boxRepo, ITreeRepo treeRepo)
{
BoxRepo = boxRepo;
TreeRepo = treeRepo;
}

public IBoxRepo BoxRepo { get; }
public ITreeRepo TreeRepo { get; }

public Task<IEnumerable<BoxAggregate>> Handle(GetBoxesQuery request, CancellationToken cancellationToken)
{
var boxAggregate = BoxRepo.GetBoxesForUser(request.OwnerId);

foreach (var item in boxAggregate)
{
var treeDefinition = TreeRepo.GetOne(item.TreeDefinitionId);
item.ChangeTreeDefinition(treeDefinition);
}

return Task.FromResult(boxAggregate);
}
}
11 changes: 11 additions & 0 deletions apps/releaf/backend/Releaf.Domain/Boxes/BoxAggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public BoxAggregate(
public IEnumerable<Seed> Seeds { get; }
public BoxVitals Vitals { get; }
public GrowthInfo GrowthInfo { get; private set; }
public TreeDefinitionAggregate TreeDefinitionAggregate { get; private set; }

public void UpdateTemperatureVitals(DateTime timeStamp, double temperature)
{
Expand Down Expand Up @@ -109,4 +110,14 @@ public void ChangeGrowthInfo(GrowthInfo growthInfo)

GrowthInfo = growthInfo;
}

public void ChangeTreeDefinition(TreeDefinitionAggregate treeDefinitionAggregate)
{
if (treeDefinitionAggregate == null)
{
throw new TreeDefinitionNotFoundException();
}

TreeDefinitionAggregate = treeDefinitionAggregate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Releaf.Shared;

namespace Releaf.Domain.Exceptions;

public class TreeDefinitionNotFoundException : DomainException
{
public TreeDefinitionNotFoundException() : base($"Tree definition was not found")
{
}
}

0 comments on commit 96cf265

Please sign in to comment.