Skip to content

Commit 8cb28e7

Browse files
feat: get raspberry (#1)
1 parent 50de649 commit 8cb28e7

File tree

13 files changed

+139
-14
lines changed

13 files changed

+139
-14
lines changed

Core/LaneSenseGuard.Core.Application/LaneSenseGuard.Core.Application.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<ProjectReference Include="..\LaneSenseGuard.Core.Domain\LaneSenseGuard.Core.Domain.csproj" />
10+
<ProjectReference Include="..\LaneSenseGuard.Core.Domain\LaneSenseGuard.Core.Domain.csproj"/>
1111
</ItemGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Phuc1403.BuildingBlock.Core.Application" Version="2024.5.31.1909" />
14+
<PackageReference Include="Phuc1403.BuildingBlock.Core.Application" Version="2024.5.31.1909"/>
1515
</ItemGroup>
1616

1717
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using AutoMapper;
2+
using BuildingBlock.Core.Application.CQRS;
3+
using BuildingBlock.Core.Domain.Repositories;
4+
using LaneSenseGuard.Core.Application.Raspberries.DTOs;
5+
using LaneSenseGuard.Core.Domain.RaspberryAggregate.Entities;
6+
7+
namespace LaneSenseGuard.Core.Application.Raspberries.CQRS.Queries.GetRaspberry;
8+
9+
public class GetRaspberryHandler : IQueryHandler<GetRaspberryQuery, RaspberryDto>
10+
{
11+
private readonly IMapper _mapper;
12+
private readonly IReadOnlyRepository<Raspberry> _readOnlyRepository;
13+
14+
public GetRaspberryHandler(IReadOnlyRepository<Raspberry> readOnlyRepository, IMapper mapper)
15+
{
16+
_readOnlyRepository = readOnlyRepository;
17+
_mapper = mapper;
18+
}
19+
20+
public async Task<RaspberryDto> Handle(GetRaspberryQuery request, CancellationToken cancellationToken)
21+
{
22+
return _mapper.Map<RaspberryDto>(await _readOnlyRepository.GetAnyAsync());
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using BuildingBlock.Core.Application.CQRS;
2+
using LaneSenseGuard.Core.Application.Raspberries.DTOs;
3+
4+
namespace LaneSenseGuard.Core.Application.Raspberries.CQRS.Queries.GetRaspberry;
5+
6+
public record GetRaspberryQuery : IQuery<RaspberryDto>;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace LaneSenseGuard.Core.Application.Raspberries.DTOs;
2+
3+
public class RaspberryDto
4+
{
5+
public Guid Id { get; set; }
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using BuildingBlock.Core.Application;
2+
using BuildingBlock.Core.Domain.Repositories;
3+
using BuildingBlock.Core.Domain.Shared.Services;
4+
using LaneSenseGuard.Core.Domain.RaspberryAggregate.Entities;
5+
using Microsoft.Extensions.Logging;
6+
7+
namespace LaneSenseGuard.Core.Application.Raspberries.Seeders;
8+
9+
public class RaspberrySeeder : IDataSeeder
10+
{
11+
private readonly ILogger<RaspberrySeeder> _logger;
12+
private readonly IOperationRepository<Raspberry> _raspberryOperationRepository;
13+
private readonly IReadOnlyRepository<Raspberry> _raspberryReadOnlyRepository;
14+
private readonly IUnitOfWork _unitOfWork;
15+
16+
public RaspberrySeeder(IOperationRepository<Raspberry> raspberryOperationRepository, IUnitOfWork unitOfWork,
17+
ILogger<RaspberrySeeder> logger, IReadOnlyRepository<Raspberry> raspberryReadOnlyRepository)
18+
{
19+
_raspberryOperationRepository = raspberryOperationRepository;
20+
_unitOfWork = unitOfWork;
21+
_logger = logger;
22+
_raspberryReadOnlyRepository = raspberryReadOnlyRepository;
23+
}
24+
25+
public async Task SeedDataAsync()
26+
{
27+
if (await _raspberryReadOnlyRepository.CheckIfExistAsync())
28+
{
29+
_logger.LogInformation("Raspberry data already seeded!");
30+
return;
31+
}
32+
33+
_logger.LogInformation("Start seeding raspberry data...");
34+
35+
36+
var raspberry = new Raspberry();
37+
38+
await _raspberryOperationRepository.AddAsync(raspberry);
39+
40+
await _unitOfWork.SaveChangesAsync();
41+
}
42+
43+
public int ExecutionOrder => 1;
44+
}

Core/LaneSenseGuard.Core.Domain/LaneSenseGuard.Core.Domain.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Phuc1403.BuildingBlock.Core.Domain" Version="2024.5.31.1909" />
10+
<PackageReference Include="Phuc1403.BuildingBlock.Core.Domain" Version="2024.5.31.1909"/>
1111
</ItemGroup>
1212

1313
</Project>

Infrastructure/LaneSenseGuard.Infrastructure.EntityFrameworkCore/DbContext.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ namespace LaneSenseGuard.Infrastructure.EntityFrameworkCore;
88

99
public class DbContext : BaseDbContext
1010
{
11-
public DbContext(DbContextOptions options, ICurrentUser currentUser, IMediator mediator) : base(options, currentUser, mediator)
11+
public DbContext(DbContextOptions options, ICurrentUser currentUser, IMediator mediator) : base(options,
12+
currentUser, mediator)
1213
{
1314
}
15+
1416
public DbSet<Raspberry> Raspberries { get; set; } = null!;
1517

1618
protected override void OnModelCreating(ModelBuilder builder)

Infrastructure/LaneSenseGuard.Infrastructure.EntityFrameworkCore/LaneSenseGuard.Infrastructure.EntityFrameworkCore.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<ProjectReference Include="..\..\Core\LaneSenseGuard.Core.Application\LaneSenseGuard.Core.Application.csproj" />
10+
<ProjectReference Include="..\..\Core\LaneSenseGuard.Core.Application\LaneSenseGuard.Core.Application.csproj"/>
1111
</ItemGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Phuc1403.BuildingBlock.Infrastructure.EntityFrameworkCore" Version="2024.5.31.1909" />
14+
<PackageReference Include="Phuc1403.BuildingBlock.Infrastructure.EntityFrameworkCore" Version="2024.5.31.1909"/>
1515
</ItemGroup>
1616

1717
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using AutoMapper;
2+
using LaneSenseGuard.Core.Application.Raspberries.DTOs;
3+
using LaneSenseGuard.Core.Domain.RaspberryAggregate.Entities;
4+
5+
namespace LaneSenseGuard.Infrastructure.EntityFrameworkCore.Mappers;
6+
7+
public class RaspberryMapper : Profile
8+
{
9+
public RaspberryMapper()
10+
{
11+
CreateMap<Raspberry, RaspberryDto>();
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using LaneSenseGuard.Core.Application.Raspberries.CQRS.Queries.GetRaspberry;
2+
using LaneSenseGuard.Core.Application.Raspberries.DTOs;
3+
using MediatR;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
namespace LaneSenseGuard.Presentation.API.Controllers;
7+
8+
[ApiController]
9+
[Route("api/raspberries")]
10+
public class RaspberryController : ControllerBase
11+
{
12+
private readonly IMediator _mediator;
13+
14+
public RaspberryController(IMediator mediator)
15+
{
16+
_mediator = mediator;
17+
}
18+
19+
[HttpGet]
20+
public async Task<ActionResult<RaspberryDto>> GetAsync()
21+
{
22+
return Ok(await _mediator.Send(new GetRaspberryQuery()));
23+
}
24+
}

Presentation/LaneSenseGuard.Presentation.API/LaneSenseGuard.Presentation.API.csproj

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@
99
<ItemGroup>
1010
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.2"/>
1111
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.3">
12-
<PrivateAssets>all</PrivateAssets>
13-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
12+
<PrivateAssets>all</PrivateAssets>
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
</PackageReference>
1515
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.3">
16-
<PrivateAssets>all</PrivateAssets>
17-
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
<PrivateAssets>all</PrivateAssets>
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1818
</PackageReference>
19-
<PackageReference Include="Phuc1403.BuildingBlock.Presentation.API" Version="2024.5.31.1909" />
19+
<PackageReference Include="Phuc1403.BuildingBlock.Presentation.API" Version="2024.5.31.1909"/>
2020
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0"/>
2121
</ItemGroup>
2222

2323
<ItemGroup>
24-
<ProjectReference Include="..\..\Infrastructure\LaneSenseGuard.Infrastructure.EntityFrameworkCore\LaneSenseGuard.Infrastructure.EntityFrameworkCore.csproj" />
24+
<ProjectReference Include="..\..\Infrastructure\LaneSenseGuard.Infrastructure.EntityFrameworkCore\LaneSenseGuard.Infrastructure.EntityFrameworkCore.csproj"/>
2525
</ItemGroup>
2626

2727
</Project>

Presentation/LaneSenseGuard.Presentation.API/Program.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
builder.Services.AddDefaultExtensions(builder.Configuration);
1313

1414
await builder.Services.AddDefaultModuleExtensionsAsync<ApplicationAssemblyReference,
15-
DomainAssemblyReference,
16-
DbContext>(builder.Configuration);
15+
DomainAssemblyReference,
16+
DbContext>(builder.Configuration);
1717

1818
builder.Host.UseDefaultHosts(builder.Configuration);
1919

Presentation/LaneSenseGuard.Presentation.API/appsettings.json

+6
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,11 @@
6767
},
6868
"ConnectionStrings": {
6969
"Redis": "localhost:6379"
70+
},
71+
"OpenApi": {
72+
"Document": {
73+
"Title": "LaneSenseGuard_WebSocket",
74+
"Version": "v1"
75+
}
7076
}
7177
}

0 commit comments

Comments
 (0)