Blazor & WASM in combination to get statistics from Spotify API for performing the song analysis. With separate microservices for auth, Spotify, user data tracking, and application, connected through gRPC with Polly.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

IdentityService.cs 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using GrpcShared.DTO;
  2. using GrpcShared.DTO.Db;
  3. using GrpcShared.Interfaces;
  4. using IdentityProvider.Models;
  5. using Microsoft.Extensions.Options;
  6. using MongoDB.Driver;
  7. namespace IdentityProvider.Services
  8. {
  9. public class IdentityService : IIdentityService
  10. {
  11. private readonly IMongoCollection<UserModel> _userCollection;
  12. private readonly IMongoCollection<TrackModel> _trackCollection;
  13. public IdentityService(IOptions<SpotifyDbConfig> spotifyDbConfig)
  14. {
  15. var mongoClient = new MongoClient(spotifyDbConfig.Value.ConnString);
  16. var mongoDbContext = mongoClient.GetDatabase(spotifyDbConfig.Value.DBName);
  17. _userCollection = mongoDbContext.GetCollection<UserModel>(spotifyDbConfig.Value.UserCollection);
  18. _trackCollection = mongoDbContext.GetCollection<TrackModel>(spotifyDbConfig.Value.TrackCollection);
  19. }
  20. public async Task<List<UserResponse>> ListUsersAsync(VoidMessage msg)
  21. {
  22. List<UserModel> users = await _userCollection.Find(_ => true).ToListAsync();
  23. //map users to match the grpc response
  24. return users.Select(u => new UserResponse
  25. {
  26. Id = u.Id,
  27. Token = u.Token
  28. }).ToList();
  29. }
  30. public async Task<UserResponse> GetTokenByIdAsync(DbRequestMessage request)
  31. {
  32. UserModel user = await _userCollection.Find(u => u.Id == request.Id).FirstOrDefaultAsync();
  33. return new UserResponse
  34. {
  35. Id = user.Id,
  36. Token = user.Token
  37. };
  38. }
  39. public async Task<TrackResponse> GetTrackByUserAsync(DbRequestMessage request)
  40. {
  41. TrackModel track = await _trackCollection.Find(t => t.UserId == request.Id).FirstOrDefaultAsync();
  42. return new TrackResponse
  43. {
  44. Id = track.Id,
  45. Album = track.Album,
  46. Artist = track.Artist,
  47. Title = track.Title
  48. };
  49. }
  50. public async Task<VoidMessage> SaveTrackAsync(SaveTrackRequest track)
  51. {
  52. TrackModel trackModel = new()
  53. {
  54. Title = track.Title,
  55. Album = track.Album,
  56. Artist = track.Artist,
  57. UserId = track.UserId
  58. };
  59. //first check if there's already a song in the db, if yes, update the row
  60. var song = await _trackCollection.FindAsync(x => x.UserId == track.UserId);
  61. if (song != null) await _trackCollection.ReplaceOneAsync(x => x.UserId == track.UserId, trackModel);
  62. else await _trackCollection.InsertOneAsync(trackModel);
  63. return new VoidMessage();
  64. }
  65. public async Task<VoidMessage> SaveUserAsync(UserResponse user)
  66. {
  67. await _userCollection.InsertOneAsync(new UserModel
  68. {
  69. Id = user.Id,
  70. Token = user.Token
  71. });
  72. return new VoidMessage();
  73. }
  74. public async Task<VoidMessage> DeleteTrackAsync(DbRequestMessage request)
  75. {
  76. await _trackCollection.DeleteOneAsync(x => x.Id == request.Id);
  77. return new VoidMessage();
  78. }
  79. public async Task<VoidMessage> DeleteUserAsync(DbRequestMessage user)
  80. {
  81. await _userCollection.DeleteOneAsync(x => x.Id == user.Id);
  82. return new VoidMessage();
  83. }
  84. }
  85. }