bitwarden-android/src/App/Services/FolderService.cs

78 lines
2.8 KiB
C#
Raw Normal View History

2016-05-02 09:52:09 +03:00
using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using Bit.App.Abstractions;
using Bit.App.Models;
using Bit.App.Models.Data;
2016-05-03 09:08:50 +03:00
using Bit.App.Models.Api;
using Newtonsoft.Json;
using System.Net.Http;
using System.Text;
2016-05-02 09:52:09 +03:00
namespace Bit.App.Services
{
2016-05-03 09:08:50 +03:00
public class FolderService : Repository<FolderData, string>, IFolderService
2016-05-02 09:52:09 +03:00
{
2016-05-03 00:50:16 +03:00
private readonly IAuthService _authService;
2016-05-03 09:08:50 +03:00
private readonly IApiService _apiService;
2016-05-02 09:52:09 +03:00
2016-05-03 00:50:16 +03:00
public FolderService(
ISqlService sqlService,
2016-05-03 09:08:50 +03:00
IAuthService authService,
IApiService apiService)
2016-05-03 00:50:16 +03:00
: base(sqlService)
2016-05-02 09:52:09 +03:00
{
2016-05-03 00:50:16 +03:00
_authService = authService;
2016-05-03 09:08:50 +03:00
_apiService = apiService;
2016-05-03 00:50:16 +03:00
}
2016-05-03 09:08:50 +03:00
public new Task<Folder> GetByIdAsync(string id)
{
var data = Connection.Table<FolderData>().Where(f => f.UserId == _authService.UserId && f.Id == id).FirstOrDefault();
return Task.FromResult(new Folder(data));
}
2016-05-03 00:50:16 +03:00
public new Task<IEnumerable<Folder>> GetAllAsync()
{
var data = Connection.Table<FolderData>().Where(f => f.UserId == _authService.UserId).Cast<FolderData>();
return Task.FromResult(data.Select(f => new Folder(f)));
2016-05-02 09:52:09 +03:00
}
2016-05-03 09:08:50 +03:00
public async Task<ApiResult<FolderResponse>> SaveAsync(Folder folder)
2016-05-02 09:52:09 +03:00
{
2016-05-03 09:08:50 +03:00
var request = new FolderRequest(folder);
var requestContent = JsonConvert.SerializeObject(request);
var requestMessage = new HttpRequestMessage
{
Method = folder.Id == null ? HttpMethod.Post : HttpMethod.Put,
RequestUri = new Uri(_apiService.Client.BaseAddress, folder.Id == null ? "/folders" : string.Concat("/folders/", folder.Id)),
Content = new StringContent(requestContent, Encoding.UTF8, "application/json")
};
requestMessage.Headers.Add("Authorization", string.Concat("Bearer ", _authService.Token));
var response = await _apiService.Client.SendAsync(requestMessage);
if(!response.IsSuccessStatusCode)
{
return await _apiService.HandleErrorAsync<FolderResponse>(response);
}
var responseContent = await response.Content.ReadAsStringAsync();
var responseObj = JsonConvert.DeserializeObject<FolderResponse>(responseContent);
var data = new FolderData(responseObj, _authService.UserId);
2016-05-02 09:52:09 +03:00
2016-05-03 09:08:50 +03:00
if(folder.Id == null)
2016-05-02 09:52:09 +03:00
{
await CreateAsync(data);
2016-05-03 09:08:50 +03:00
folder.Id = responseObj.Id;
2016-05-02 09:52:09 +03:00
}
else
{
await ReplaceAsync(data);
}
2016-05-03 09:08:50 +03:00
return ApiResult<FolderResponse>.Success(responseObj, response.StatusCode);
2016-05-02 09:52:09 +03:00
}
}
}