MCPcopy Create free account
hub / github.com/Azure-Samples/todo-csharp-cosmos-sql / ListsRepository

Class ListsRepository

src/api/ListsRepository.cs:6–115  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4namespace SimpleTodo.Api;
5
6public class ListsRepository
7{
8 private readonly Container _listsCollection;
9 private readonly Container _itemsCollection;
10
11 public ListsRepository(CosmosClient client, IConfiguration configuration)
12 {
13 var database = client.GetDatabase(configuration["AZURE_COSMOS_DATABASE_NAME"]);
14 _listsCollection = database.GetContainer("TodoList");
15 _itemsCollection = database.GetContainer("TodoItem");
16 }
17
18 public async Task<IEnumerable<TodoList>> GetListsAsync(int? skip, int? batchSize)
19 {
20 return await ToListAsync(
21 _listsCollection.GetItemLinqQueryable<TodoList>(),
22 skip,
23 batchSize);
24 }
25
26 public async Task<TodoList?> GetListAsync(string listId)
27 {
28 var response = await _listsCollection.ReadItemAsync<TodoList>(listId, new PartitionKey(listId));
29 return response?.Resource;
30 }
31
32 public async Task DeleteListAsync(string listId)
33 {
34 await _listsCollection.DeleteItemAsync<TodoList>(listId, new PartitionKey(listId));
35 }
36
37 public async Task AddListAsync(TodoList list)
38 {
39 list.Id = Guid.NewGuid().ToString("N");
40 await _listsCollection.UpsertItemAsync(list, new PartitionKey(list.Id));
41 }
42
43 public async Task UpdateList(TodoList existingList)
44 {
45 await _listsCollection.ReplaceItemAsync(existingList, existingList.Id, new PartitionKey(existingList.Id));
46 }
47
48 public async Task<IEnumerable<TodoItem>> GetListItemsAsync(string listId, int? skip, int? batchSize)
49 {
50 return await ToListAsync(
51 _itemsCollection.GetItemLinqQueryable<TodoItem>().Where(i => i.ListId == listId),
52 skip,
53 batchSize);
54 }
55
56 public async Task<IEnumerable<TodoItem>> GetListItemsByStateAsync(string listId, string state, int? skip, int? batchSize)
57 {
58 return await ToListAsync(
59 _itemsCollection.GetItemLinqQueryable<TodoItem>().Where(i => i.ListId == listId && i.State == state),
60 skip,
61 batchSize);
62 }
63

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected