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;
|
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)
|
2016-05-03 01:35:01 +03:00
|
|
|
|
{
|
|
|
|
|
var data = Connection.Table<FolderData>().Where(f => f.UserId == _authService.UserId && f.Id == id).FirstOrDefault();
|
2016-05-04 02:49:49 +03:00
|
|
|
|
var folder = new Folder(data);
|
|
|
|
|
return Task.FromResult(folder);
|
2016-05-03 01:35:01 +03:00
|
|
|
|
}
|
|
|
|
|
|
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>();
|
2016-05-04 02:49:49 +03:00
|
|
|
|
var folders = data.Select(f => new Folder(f));
|
|
|
|
|
return Task.FromResult(folders);
|
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);
|
2016-05-04 02:49:49 +03:00
|
|
|
|
var requestMessage = new TokenHttpRequestMessage(request)
|
2016-05-03 09:08:50 +03:00
|
|
|
|
{
|
|
|
|
|
Method = folder.Id == null ? HttpMethod.Post : HttpMethod.Put,
|
2016-05-04 02:49:49 +03:00
|
|
|
|
RequestUri = new Uri(_apiService.Client.BaseAddress, folder.Id == null ? "/folders" : $"/folders/{folder.Id}"),
|
2016-05-03 09:08:50 +03:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|