Skip to content

Commit eddd59f

Browse files
committed
add gRPC
1 parent 8a6560a commit eddd59f

4 files changed

Lines changed: 78 additions & 12 deletions

File tree

Protos/user.proto

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
11
syntax = "proto3";
22

33
service UserGrpc {
4-
rpc GetUser (UserRequest) returns (UserResponse);
4+
rpc GetUser (UserRequest) returns (UserResponse);
5+
rpc GetUsers (UserListRequest) returns (UserListResponse);
56
}
67

78
message UserRequest {
8-
string id = 1;
9+
string id = 1;
10+
}
11+
12+
message UserListRequest {
13+
repeated string ids = 1;
914
}
1015

1116
message UserResponse {
12-
string id = 1;
13-
string name = 2;
14-
string email = 3;
15-
string legend = 4;
16-
string avatar = 5;
17-
string login = 6;
18-
int32 reputationScore = 7;
17+
string name = 2;
18+
string email = 3;
19+
string legend = 4;
20+
string avatar = 5;
21+
string login = 6;
22+
int32 reputationScore = 7;
23+
}
24+
25+
message UserListResponse {
26+
repeated UserResponse users = 1;
1927
}

User/userGrpcService.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,35 @@ public override async Task<UserResponse> GetUser(UserRequest request, ServerCall
1616

1717
return new UserResponse
1818
{
19-
Id = request.Id,
2019
Name = user.Name,
2120
Legend = user.Legend,
2221
Avatar = user.Avatar,
2322
Login = user.Login,
2423
ReputationScore = user.ReputationScore
2524
};
2625
}
26+
27+
public override async Task<UserListResponse> GetUsers(UserListRequest request, ServerCallContext context)
28+
{
29+
30+
string[] userIds = request.Ids.ToArray();
31+
32+
var result = await _userService.GetUsersInfo(userIds);
33+
34+
UserListResponse response = new UserListResponse();
35+
36+
foreach(var user in result)
37+
{
38+
response.Users.Add(new UserResponse
39+
{
40+
Name = user.Name,
41+
Legend = user.Legend,
42+
Avatar = user.Avatar,
43+
Login = user.Login,
44+
ReputationScore = user.ReputationScore
45+
});
46+
}
47+
48+
return response;
49+
}
2750
}

User/userRepo.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using Microsoft.EntityFrameworkCore;
2+
using System.Collections.Generic;
23

34
public interface IUserRepo
45
{
56
Task<User?> GetUser(string login, string password);
67
Task<User?> GetUser(string userId);
8+
Task<List<User>?> GetUsers(string[] userId);
79
Task CreateUser(User UserData);
810
Task<bool> DeleteUser(string login);
911
Task<string> GetRefreshTokenHashByUserId(Guid userId);
@@ -35,6 +37,11 @@ public UserRepo(ApplicationDbContext context)
3537
return await _context.Users.Where(e => e.UserId == Guid.Parse(userId)).FirstOrDefaultAsync();
3638
}
3739

40+
public async Task<List<User>?> GetUsers(string[] userIds)
41+
{
42+
return await _context.Users.Where(e => userIds.Contains(e.Id.ToString())).ToListAsync();
43+
}
44+
3845
public async Task CreateUser(User UserData)
3946
{
4047
try

User/userService.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using DeveSecurity;
2-
using Microsoft.AspNetCore.Http.HttpResults;
3-
using Microsoft.Extensions.Configuration.UserSecrets;
42

53
public class ICreateUser
64
{
@@ -18,6 +16,7 @@ public interface IUserService
1816
public Task<bool> DeleteUser(string login);
1917
public Task<RefreshTokenResponseDto?> RefreshAccessToken(RefreshTokenRequestDto data);
2018
public Task<GetUserInfoDto> GetUserInfo(string userId);
19+
public Task<List<GetUserInfoDto>> GetUsersInfo(string[] userIds);
2120

2221
}
2322

@@ -160,4 +159,33 @@ public async Task<GetUserInfoDto> GetUserInfo(string userId)
160159
throw new Exception(e.Message);
161160
}
162161
}
162+
163+
public async Task<List<GetUserInfoDto>> GetUsersInfo(string[] userIds)
164+
{
165+
try
166+
{
167+
var result = await _userRepo.GetUsers(userIds);
168+
169+
List<GetUserInfoDto> users = [];
170+
171+
foreach(var user in result!)
172+
{
173+
users.Add(new GetUserInfoDto
174+
{
175+
Name = user!.Name,
176+
Login = user!.Login,
177+
Legend = user!.Legend,
178+
Avatar = user!.AvatarUrl,
179+
ReputationScore = user!.ReputationScore
180+
});
181+
}
182+
183+
return users;
184+
185+
}
186+
catch(Exception e)
187+
{
188+
throw new Exception(e.Message);
189+
}
190+
}
163191
}

0 commit comments

Comments
 (0)