-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserService.cs
More file actions
57 lines (50 loc) · 1.85 KB
/
Copy pathUserService.cs
File metadata and controls
57 lines (50 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using Application.DTOs.Stats;
using Application.Interfaces.Services;
using Domain.Entities;
using Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
namespace Infrastructure.Services;
public class UserService(DiscordBotContext context) : IUserService
{
public async Task<User?> GetUserByUsernameAsync(string username)
{
var user = await context.Users
.FirstOrDefaultAsync(u => u.Username == username);
return user;
}
public async Task<User?> GetUserByDisplayNameAsync(string displayName)
{
var user = await context.Users
.FirstOrDefaultAsync(u => u.DisplayName == displayName);
return user;
}
public async Task<ICollection<UserStatsDto>> GetAllUsersAsync()
{
var users = await context.Users
.Select(u => new UserStatsDto
{
Username = u.Username,
MemberSince = u.CreatedAt,
TotalPlays = u.PlayHistories.Sum(ph => ph.TotalPlays),
UniqueSongs = u.PlayHistories.Select(ph => ph.Song).Distinct().Count(),
LastPlayed = u.PlayHistories
.OrderByDescending(ph => ph.PlayedAt)
.Select(ph => (DateTimeOffset?)ph.PlayedAt)
.FirstOrDefault(),
DisplayName = u.DisplayName ?? u.Username,
RecentSongs = u.PlayHistories
.OrderByDescending(ph => ph.PlayedAt)
.Take(20)
.Select(ph => new RecentSongDto
{
Title = ph.Song.Title,
TotalPlays = ph.TotalPlays,
PlayedAt = ph.PlayedAt
})
.ToList()
})
.OrderByDescending(u => u.TotalPlays)
.ToListAsync();
return users;
}
}