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;
|
2016-07-20 01:46:39 +03:00
|
|
|
|
using Xamarin.Forms;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
|
|
|
|
namespace Bit.App.Services
|
|
|
|
|
{
|
2016-05-06 07:17:38 +03:00
|
|
|
|
public class FolderService : IFolderService
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-05-06 07:17:38 +03:00
|
|
|
|
private readonly IFolderRepository _folderRepository;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
private readonly IAuthService _authService;
|
2016-05-06 07:17:38 +03:00
|
|
|
|
private readonly IFolderApiRepository _folderApiRepository;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-05-03 00:50:16 +03:00
|
|
|
|
public FolderService(
|
2016-05-06 07:17:38 +03:00
|
|
|
|
IFolderRepository folderRepository,
|
2016-05-03 09:08:50 +03:00
|
|
|
|
IAuthService authService,
|
2016-05-06 07:17:38 +03:00
|
|
|
|
IFolderApiRepository folderApiRepository)
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-05-06 07:17:38 +03:00
|
|
|
|
_folderRepository = folderRepository;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
_authService = authService;
|
2016-05-06 07:17:38 +03:00
|
|
|
|
_folderApiRepository = folderApiRepository;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 07:17:38 +03:00
|
|
|
|
public async Task<Folder> GetByIdAsync(string id)
|
2016-05-03 01:35:01 +03:00
|
|
|
|
{
|
2016-05-06 07:17:38 +03:00
|
|
|
|
var data = await _folderRepository.GetByIdAsync(id);
|
|
|
|
|
if(data == null || data.UserId != _authService.UserId)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-04 02:49:49 +03:00
|
|
|
|
var folder = new Folder(data);
|
2016-05-06 07:17:38 +03:00
|
|
|
|
return folder;
|
2016-05-03 01:35:01 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 07:17:38 +03:00
|
|
|
|
public async Task<IEnumerable<Folder>> GetAllAsync()
|
2016-05-03 00:50:16 +03:00
|
|
|
|
{
|
2016-05-06 07:17:38 +03:00
|
|
|
|
var data = await _folderRepository.GetAllByUserIdAsync(_authService.UserId);
|
2016-05-04 02:49:49 +03:00
|
|
|
|
var folders = data.Select(f => new Folder(f));
|
2016-05-06 07:17:38 +03:00
|
|
|
|
return 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-06 07:17:38 +03:00
|
|
|
|
ApiResult<FolderResponse> response = null;
|
2016-05-03 09:08:50 +03:00
|
|
|
|
var request = new FolderRequest(folder);
|
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
|
|
|
|
{
|
2016-05-06 07:17:38 +03:00
|
|
|
|
response = await _folderApiRepository.PostAsync(request);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-05-06 07:17:38 +03:00
|
|
|
|
response = await _folderApiRepository.PutAsync(folder.Id, request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(response.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
var data = new FolderData(response.Result, _authService.UserId);
|
|
|
|
|
if(folder.Id == null)
|
|
|
|
|
{
|
|
|
|
|
await _folderRepository.InsertAsync(data);
|
|
|
|
|
folder.Id = data.Id;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await _folderRepository.UpdateAsync(data);
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
2016-07-20 01:46:39 +03:00
|
|
|
|
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|
|
|
|
|
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
|
|
|
|
|
{
|
|
|
|
|
MessagingCenter.Send(Application.Current, "Logout", (string)null);
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-05-06 07:17:38 +03:00
|
|
|
|
return response;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
2016-05-18 06:25:40 +03:00
|
|
|
|
|
2016-07-02 02:06:07 +03:00
|
|
|
|
public async Task<ApiResult> DeleteAsync(string folderId)
|
2016-05-18 06:25:40 +03:00
|
|
|
|
{
|
2016-07-02 02:06:07 +03:00
|
|
|
|
var response = await _folderApiRepository.DeleteAsync(folderId);
|
2016-05-18 06:25:40 +03:00
|
|
|
|
if(response.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
await _folderRepository.DeleteAsync(folderId);
|
|
|
|
|
}
|
2016-07-20 01:46:39 +03:00
|
|
|
|
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|
|
|
|
|
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
|
|
|
|
|
{
|
|
|
|
|
MessagingCenter.Send(Application.Current, "Logout", (string)null);
|
|
|
|
|
}
|
2016-05-18 06:25:40 +03:00
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|