drinkMe/drinkMe/Client/Services/DataService.cs

48 lines
1.7 KiB
C#

using collAnon.Pub.Shared;
using drinkMe.Client.Services.Interfaces;
using drinkMe.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
namespace drinkMe.Client.Services
{
public class DataService : IDataService
{
readonly HttpClient HttpClient;
public DataService(HttpClient httpClient)
{
HttpClient = httpClient;
}
public async Task<WebResult> IsValidDiscountCode(string code)
{
var result = new WebResult();
var httpResponse = await HttpClient.GetAsync($"api/Drinks/{nameof(IsValidDiscountCode)}?{nameof(code)}={code}");
if (!httpResponse.IsSuccessStatusCode)
return result.Invalidate(JsonSerializer.Deserialize<WebResult>(await httpResponse.Content.ReadAsStringAsync(), new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }));
result.Data = JsonSerializer.Deserialize<DiscountCodeViewModel>(await httpResponse.Content.ReadAsStringAsync(), new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
return result;
}
public async Task<WebResult> GetDrinks()
{
var result = new WebResult();
var httpResponse = await HttpClient.GetAsync($"api/Drinks/{nameof(GetDrinks)}");
if (!httpResponse.IsSuccessStatusCode)
return result.Invalidate(JsonSerializer.Deserialize<WebResult>(await httpResponse.Content.ReadAsStringAsync(), new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }));
result.Data = JsonSerializer.Deserialize<List<DrinkViewModel>>(await httpResponse.Content.ReadAsStringAsync(), new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
return result;
}
}
}