Skip to content

Instantly share code, notes, and snippets.

@mattbrailsford
Created August 3, 2022 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattbrailsford/ef8acb7bdd054b83bef568075bbfe79c to your computer and use it in GitHub Desktop.
Save mattbrailsford/ef8acb7bdd054b83bef568075bbfe79c to your computer and use it in GitHub Desktop.
REST Konstrukt Repository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text.Json;
using Konstrukt.Persistence;
using Konstrukt.Web.Site.Models;
using Umbraco.Cms.Core.Models;
namespace Konstrukt.Web.Site.Models
{
public class Todo
{
public int Id { get; set; }
public string Title { get; set; }
public bool Completed { get; set; }
}
public class TestScopedDep
{
public string Test { get; set; } = "Hello World";
}
}
namespace Konstrukt.Web.Site.Repositories
{
public class TodosRepository : KonstruktRepository<Todo, int>
{
private static readonly HttpClient _client = new HttpClient();
public TodosRepository(KonstruktRepositoryContext context, TestScopedDep dep)
: base(context)
{
_client.DefaultRequestHeaders.Accept.Clear();
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
protected override int GetIdImpl(Todo entity)
=> entity.Id;
protected override Todo GetImpl(int id)
{
var json = _client.GetStringAsync("https://jsonplaceholder.typicode.com/todos/" + id).Result;
return JsonSerializer.Deserialize<Todo>(json, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
}
protected override Todo SaveImpl(Todo entity)
{
throw new NotImplementedException();
}
protected override void DeleteImpl(int id)
{
throw new NotImplementedException();
}
protected override IEnumerable<Todo> GetAllImpl(Expression<Func<Todo, bool>> whereClause, Expression<Func<Todo, object>> orderBy, SortDirection orderByDirection)
{
return DoGetAll(whereClause, orderBy, orderByDirection);
}
protected override PagedResult<Todo> GetPagedImpl(int pageNumber, int pageSize, Expression<Func<Todo, bool>> whereClause, Expression<Func<Todo, object>> orderBy,
SortDirection orderByDirection)
{
var all = DoGetAll(whereClause, orderBy, orderByDirection);
return new PagedResult<Todo>(all.Count, pageNumber, pageSize)
{
Items = all.Skip((pageNumber - 1) * pageSize).Take(pageSize)
};
}
protected override long GetCountImpl(Expression<Func<Todo, bool>> whereClause)
{
return DoGetAll(whereClause).Count;
}
private List<Todo> DoGetAll(Expression<Func<Todo, bool>> whereClause,
Expression<Func<Todo, object>> orderBy = null,
SortDirection orderByDirection = SortDirection.Ascending)
{
var json = _client.GetStringAsync("https://jsonplaceholder.typicode.com/todos").Result;
var all = JsonSerializer.Deserialize<List<Todo>>(json, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
}) ?? new List<Todo>();
if(whereClause != null)
all = all.Where(whereClause.Compile()).ToList();
if (orderBy != null)
all = (orderByDirection == SortDirection.Ascending
? all.OrderBy(orderBy.Compile())
: all.OrderByDescending(orderBy.Compile())).ToList();
return all;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment