Skip to content

Commit

Permalink
ToDoItemsClient added all other methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Hijtec committed Nov 20, 2024
1 parent 42f3dbf commit 7fb1084
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 11 deletions.
4 changes: 4 additions & 0 deletions ToDoList/src/ToDoList.Frontend/Clients/IToDoItemsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@ namespace ToDoList.Frontend.Clients;

public interface IToDoItemsClient
{
public Task CreateItemAsync(ToDoItemView itemView);
public Task<ToDoItemView?> ReadItemByIdAsync(int itemId);
public Task<List<ToDoItemView>> ReadItemsAsync();
public Task DeleteItemAsync(ToDoItemView itemView);
public Task UpdateItemAsync(ToDoItemView itemView);
}
123 changes: 112 additions & 11 deletions ToDoList/src/ToDoList.Frontend/Clients/ToDoItemsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,122 @@ namespace ToDoList.Frontend.Clients;
using ToDoList.Domain.DTOs;
using ToDoList.Frontend.Views;

public class ToDoItemsClient(HttpClient httpClient) : IToDoItemsClient
public class ToDoItemsClient(HttpClient httpClient) : IToDoItemsClient //primary constructor
{
public async Task<List<ToDoItemView>> ReadItemsAsync()
public async Task CreateItemAsync(ToDoItemView itemView)
{
var itemRequest = new ToDoItemCreateRequestDto(itemView.Name, itemView.Description, itemView.IsCompleted);
try
{
var response = await httpClient.PostAsJsonAsync("api/ToDoItems", itemRequest);
if (response.StatusCode == System.Net.HttpStatusCode.Created)
{
Console.WriteLine($"POST request successful: Created a ToDoItem.");
return;
}
else
{
Console.WriteLine($"POST request failed with status code: {response.StatusCode}");
}
}
catch (Exception e)
{
Console.WriteLine($"Exception occured: {e.Message}");
}
}

public async Task DeleteItemAsync(ToDoItemView itemView)
{
try
{
var response = await httpClient.DeleteAsync($"api/ToDoItems/{itemView.ToDoItemId}");
if (response.StatusCode == System.Net.HttpStatusCode.NoContent)
{
Console.WriteLine($"DELETE request successful: Deleted ToDoItem with id {itemView.ToDoItemId}.");
return;
}
else
{
Console.WriteLine($"DELETE request failed with status code: {response.StatusCode}");
}
}
catch (Exception e)
{
Console.WriteLine($"Exception occured: {e.Message}");
}
}

public async Task<ToDoItemView?> ReadItemByIdAsync(int itemId)
{
var toDoItemsView = new List<ToDoItemView>();
var response = await httpClient.GetFromJsonAsync<List<ToDoItemGetResponseDto>>("api/ToDoItems");
try
{
var response = await httpClient.GetFromJsonAsync<ToDoItemGetResponseDto>($"api/TodoItems/{itemId}");
if (response is null)
{
Console.WriteLine($"GET request failed: Item with {itemId} id not found.");
throw new ArgumentException($"Given id {itemId} does not exist.");
}

toDoItemsView = response.Select(dto => new ToDoItemView
return new()
{
ToDoItemId = response.Id,
Name = response.Name,
Description = response.Description,
IsCompleted = response.IsCompleted,
};
}
catch (Exception e)
{
ToDoItemId = dto.Id,
Name = dto.Name,
Description = dto.Description,
IsCompleted = dto.IsCompleted
}).ToList();
Console.WriteLine($"Exception occured: {e.Message}");
return null;
}
}

return toDoItemsView;
public async Task<List<ToDoItemView>> ReadItemsAsync()
{
var toDoItemViews = new List<ToDoItemView>();
try
{
var response = await httpClient.GetFromJsonAsync<List<ToDoItemGetResponseDto>>("api/ToDoItems");
if (response is null)
{
Console.WriteLine($"GET request failed: No items to read.");
return toDoItemViews;
}
toDoItemViews = response.Select(dto => new ToDoItemView
{
ToDoItemId = dto.Id,
Name = dto.Name,
Description = dto.Description,
IsCompleted = dto.IsCompleted
}).ToList();
}
catch (Exception e)
{
Console.WriteLine($"Exception occured: {e.Message}");
}

return toDoItemViews;
}
public async Task UpdateItemAsync(ToDoItemView itemView)
{
try
{
var itemRequest = new ToDoItemUpdateRequestDto(itemView.Name, itemView.Description, itemView.IsCompleted);
var response = await httpClient.PutAsJsonAsync($"api/ToDoItems/{itemView.ToDoItemId}", itemRequest);
if (response.StatusCode == System.Net.HttpStatusCode.NoContent)
{
Console.WriteLine($"PUT request successful: Updated ToDoItem with id {itemView.ToDoItemId}.");
return;
}
else
{
Console.WriteLine($"PUT request failed: {response.StatusCode}");
}
}
catch (Exception e)
{
Console.WriteLine($"Exception occured: {e.Message}");
}
}
}

0 comments on commit 7fb1084

Please sign in to comment.