Made more async goodness

This commit is contained in:
tidusjar 2016-05-27 15:07:07 +01:00
parent 8550cc4c5e
commit 166e0f81cf
7 changed files with 181 additions and 30 deletions

View file

@ -27,6 +27,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
@ -57,6 +58,19 @@ namespace PlexRequests.Core
return result ? id : -1;
}
public async Task<int> AddRequestAsync(RequestedModel model)
{
var entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId };
var id = await Repo.InsertAsync(entity);
model.Id = id;
entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId, Id = id, MusicId = model.MusicBrainzId };
var result = await Repo.UpdateAsync(entity);
return result ? id : -1;
}
public RequestedModel CheckRequest(int providerId)
{
var blobs = Repo.GetAll();
@ -77,12 +91,24 @@ namespace PlexRequests.Core
Repo.Delete(blob);
}
public async Task DeleteRequestAsync(RequestedModel request)
{
var blob = await Repo.GetAsync(request.Id);
await Repo.DeleteAsync(blob);
}
public bool UpdateRequest(RequestedModel model)
{
var entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId, Id = model.Id };
return Repo.Update(entity);
}
public async Task<bool> UpdateRequestAsync(RequestedModel model)
{
var entity = new RequestBlobs { Type = model.Type, Content = ByteConverterHelper.ReturnBytes(model), ProviderId = model.ProviderId, Id = model.Id };
return await Repo.UpdateAsync(entity);
}
public RequestedModel Get(int id)
{
var blob = Repo.Get(id);
@ -94,6 +120,17 @@ namespace PlexRequests.Core
return model;
}
public async Task<RequestedModel> GetAsync(int id)
{
var blob = await Repo.GetAsync(id);
if (blob == null)
{
return new RequestedModel();
}
var model = ByteConverterHelper.ReturnObject<RequestedModel>(blob.Content);
return model;
}
public IEnumerable<RequestedModel> GetAll()
{
var blobs = Repo.GetAll();
@ -102,6 +139,14 @@ namespace PlexRequests.Core
.ToList();
}
public async Task<IEnumerable<RequestedModel>> GetAllAsync()
{
var blobs = await Repo.GetAllAsync();
return blobs.Select(b => Encoding.UTF8.GetString(b.Content))
.Select(JsonConvert.DeserializeObject<RequestedModel>)
.ToList();
}
public bool BatchUpdate(List<RequestedModel> model)
{
var entities = model.Select(m => new RequestBlobs { Type = m.Type, Content = ByteConverterHelper.ReturnBytes(m), ProviderId = m.ProviderId, Id = m.Id }).ToList();