Skip to content

Commit 6612099

Browse files
list users api impl
1 parent 0d88e7e commit 6612099

File tree

6 files changed

+48
-28
lines changed

6 files changed

+48
-28
lines changed

src/Microstack.API/Abstractions/IUserService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public interface IUserService
1010
{
1111
public Task<IList<Profile>> GetProfiles(string userId);
1212
public Task PersistProfile(string userId, Profile profile);
13+
Task<IReadOnlyList<string>> GetUsers();
1314
}
1415
}

src/Microstack.API/Controllers/UsersController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public UsersController(IUserService userService)
2020
_userService = userService;
2121
}
2222

23+
[HttpGet]
24+
public async Task<IActionResult> Get() => Ok(await _userService.GetUsers());
25+
2326
[HttpGet("{userId}")]
2427
public async Task<IActionResult> Get(string userId)
2528
{

src/Microstack.API/Services/UserService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public async Task<IList<Profile>> GetProfiles(string userId)
2424
return profiles.MapFromPersistenceModel();
2525
}
2626

27+
public Task<IReadOnlyList<string>> GetUsers()
28+
{
29+
return _persistenceProvider.GetUsers();
30+
}
31+
2732
public async Task PersistProfile(string userId, Profile profile)
2833
{
2934
await _persistenceProvider.PersistProfile(userId, profile.MapToRepositoryModel());

src/Microstack.CLI/Commands/SubCommands/Users.cs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,6 @@ public class Users : BaseCommand
3636
ValueName = "UserId")]
3737
public string UserId { get; set; }
3838

39-
[Option(
40-
CommandOptionType.NoValue,
41-
Description = "List user profiles",
42-
LongName = "profiles",
43-
ShortName = "p",
44-
ShowInHelpText = true
45-
)]
46-
public bool ShowProfiles { get; set; }
47-
4839
[Option(
4940
CommandOptionType.SingleValue,
5041
Description = "Add API Url",
@@ -84,32 +75,44 @@ protected override async Task<int> OnExecute(CommandLineApplication app)
8475

8576
if (!string.IsNullOrWhiteSpace(UserId))
8677
{
87-
if (ShowProfiles)
78+
79+
using (var client = new HttpClient())
8880
{
89-
using (var client = new HttpClient())
81+
client.BaseAddress = new Uri(settings);
82+
try
83+
{
84+
var response = await (await client.GetAsync($"/api/users/{UserId}")).Content.ReadAsStringAsync();
85+
var formattedJson = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(response), Formatting.Indented);
86+
OuputToConsole(formattedJson);
87+
return 0;
88+
}
89+
catch (Exception ex)
9090
{
91-
client.BaseAddress = new Uri(settings);
92-
try
93-
{
94-
var response = await (await client.GetAsync($"/api/users/{UserId}")).Content.ReadAsStringAsync();
95-
var formattedJson = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(response), Formatting.Indented);
96-
OuputToConsole(formattedJson);
97-
return 0;
98-
}
99-
catch (Exception ex)
100-
{
101-
OutputError($"Error occurred while connecting to api {ex.Message}");
102-
return 1;
103-
}
91+
OutputError($"Error occurred while connecting to api {ex.Message}");
92+
return 1;
10493
}
10594
}
106-
else
95+
}
96+
if (All)
97+
{
98+
using (var client = new HttpClient())
10799
{
108-
app.ShowHelp();
109-
return 1;
100+
client.BaseAddress = new Uri(settings);
101+
try
102+
{
103+
var response = await (await client.GetAsync($"/api/users")).Content.ReadAsStringAsync();
104+
var formattedJson = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(response), Formatting.Indented);
105+
OuputToConsole(formattedJson);
106+
return 0;
107+
}
108+
catch (Exception ex)
109+
{
110+
OutputError($"Error occurred while connecting to api {ex.Message}");
111+
return 1;
112+
}
110113
}
111114
}
112-
115+
113116
app.ShowHelp();
114117
return 0;
115118
}

src/Microstack.Repository/Abstractions/IPersistenceProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public interface IPersistenceProvider
1010
{
1111
Task<List<Profile>> GetProfilesForUser(string userId);
1212
Task PersistProfile(string userId, Profile profile);
13+
Task<IReadOnlyList<string>> GetUsers();
1314
}
1415
}

src/Microstack.Repository/Providers/MongoProvider.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,12 @@ public async Task PersistProfile(string userId, Profile profile)
3434

3535
await userProfiles.UpdateOneAsync(filter, update, new UpdateOptions() { IsUpsert = true });
3636
}
37+
38+
public async Task<IReadOnlyList<string>> GetUsers()
39+
{
40+
var userProfiles = _database.GetCollection<User>("user.profiles");
41+
var users = await (userProfiles.Find(FilterDefinition<User>.Empty).Project(f => f.UserId).ToListAsync());
42+
return users;
43+
}
3744
}
3845
}

0 commit comments

Comments
 (0)