Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stats #19

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Core/Services/CollectionsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ private async Task<Collection> VerifyCollectionExistsAndUserHasAccess(Guid colle
if (collection.UserEmail != loggedInUser) throw new NoAccessException("You don't have access to this collection");
return collection;
}

public async Task<int> GetTotalCollectionsCount(string email)
{
return await collectionsRepository.GetTotalCollectionsCount(email);
}
}
10 changes: 10 additions & 0 deletions Core/Services/PlantService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ public async Task<List<GetCriticalPlantDto>> GetCriticalPlants(string requesterE

return criticalPlants;
}

public async Task<int> GetHappyPlantsCount(string userEmail)
{
return await plantRepository.GetHappyPlantsCount(userEmail);
}

public async Task<int> GetTotalPlantsCount(string userEmail)
{
return await plantRepository.GetTotalPlantsCount(userEmail);
}

private string GenerateRandomNickname()
{
Expand Down
7 changes: 7 additions & 0 deletions Infrastructure/Repositories/CollectionsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,11 @@ public async Task RemovePlantFromCollection(Collection collection, Plant plant)
collection.Plants.Remove(plant);
await applicationDbContext.SaveChangesAsync();
}

public async Task<int> GetTotalCollectionsCount(string email)
{
await using var applicationDbContext = await dbContextFactory.CreateDbContextAsync();
return await applicationDbContext.Collections
.CountAsync(collection => collection.UserEmail == email);
}
}
16 changes: 16 additions & 0 deletions Infrastructure/Repositories/PlantRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,26 @@
.ThenByDescending(log => log.TimeStamp)
.FirstOrDefault()
})
.OrderBy(p => p.WorstMood.Mood)

Check warning on line 111 in Infrastructure/Repositories/PlantRepository.cs

View workflow job for this annotation

GitHub Actions / tests

Dereference of a possibly null reference.
.ThenByDescending(p => p.WorstMood.TimeStamp)

Check warning on line 112 in Infrastructure/Repositories/PlantRepository.cs

View workflow job for this annotation

GitHub Actions / tests

Dereference of a possibly null reference.
.Take(3)
.Select(p => p.Plant)
.ToListAsync();
}

public async Task<int> GetHappyPlantsCount(string userEmail)
{
await using var context = await dbContextFactory.CreateDbContextAsync();
return await context.Plants
.Include(plant => plant.ConditionsLogs)
.Where(p => p.UserEmail == userEmail && p.ConditionsLogs.Count != 0)
.CountAsync(p => p.ConditionsLogs.OrderByDescending(log => log.TimeStamp).FirstOrDefault()!.Mood > 2);
}

public async Task<int> GetTotalPlantsCount(string userEmail)
{
await using var context = await dbContextFactory.CreateDbContextAsync();
return await context.Plants
.CountAsync(p => p.UserEmail == userEmail);
}
}
5 changes: 1 addition & 4 deletions api/Events/Auth/Client/ClientWantsToLogIn.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using api.Events.Auth.Server;
using api.Events.Global;
using api.Extensions;
using Core.Services;
using Core.Services.External.BlobStorage;
Expand Down Expand Up @@ -48,7 +49,3 @@ public override async Task Handle(ClientWantsToLogInDto dto, IWebSocketConnectio
}
}

public class ServerSendsUserInfo : BaseDto
{
public GetUserDto GetUserDto { get; set; } = null!;
}
9 changes: 9 additions & 0 deletions api/Events/Global/ServerSendsUserInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using lib;
using Shared.Dtos;

namespace api.Events.Global;

public class ServerSendsUserInfo : BaseDto
{
public GetUserDto GetUserDto { get; set; } = null!;
}
48 changes: 48 additions & 0 deletions api/Events/Stats/ClientWantsStats.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using api.Extensions;
using Core.Services;
using Fleck;
using lib;
using Shared.Models;

namespace api.Events.Stats;

public class ClientWantsStatsDto : BaseDtoWithJwt
{

}

public class ClientWantsStats(PlantService plantService, CollectionsService collectionsService, JwtService jwtService) : BaseEventHandler<ClientWantsStatsDto>
{
public override async Task Handle(ClientWantsStatsDto dto, IWebSocketConnection socket)
{
var email = jwtService.GetEmailFromJwt(dto.Jwt!);

var totalPlants = await plantService.GetTotalPlantsCount(email);
var happyPlants = await plantService.GetHappyPlantsCount(email);
var collections = await collectionsService.GetTotalCollectionsCount(email);

var statsDto = new ServerSendsStats
{
Stats = new Stats
{
TotalPlants = totalPlants,
HappyPlants = happyPlants,
Collections = collections
}
};

socket.SendDto(statsDto);
}
}

public class ServerSendsStats : BaseDto
{
public Stats Stats { get; set; }
}

public class Stats
{
public int TotalPlants { get; set; }
public int HappyPlants { get; set; }
public int Collections { get; set; }
}
38 changes: 38 additions & 0 deletions api/Events/User/ClientWantsUserInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using api.Events.Global;
using api.Extensions;
using Core.Services;
using Core.Services.External.BlobStorage;
using Fleck;
using lib;
using Shared.Dtos;
using Shared.Models;

namespace api.Events.User;

public class ClientWantsUserInfoDto : BaseDtoWithJwt
{

}

public class ClientWantsUserInfo(UserService userService, JwtService jwtService, IBlobStorageService blobStorageService)
: BaseEventHandler<ClientWantsUserInfoDto>
{
public override async Task Handle(ClientWantsUserInfoDto dto, IWebSocketConnection socket)
{
var email = jwtService.GetEmailFromJwt(dto.Jwt);
var user = await userService.GetUserByEmail(email);
var getUserDto = new GetUserDto
{
UserEmail = user.UserEmail,
Username = user.UserName
};
if (user.BlobUrl != null)
{
getUserDto.BlobUrl = blobStorageService.GenerateSasUri(user.BlobUrl, false);
}
socket.SendDto(new ServerSendsUserInfo
{
GetUserDto = getUserDto
});
}
}
Loading