bitwarden-android/src/App/Services/FolderService.cs
2016-05-17 23:25:40 -04:00

89 lines
2.6 KiB
C#

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;
using Bit.App.Models.Api;
namespace Bit.App.Services
{
public class FolderService : IFolderService
{
private readonly IFolderRepository _folderRepository;
private readonly IAuthService _authService;
private readonly IFolderApiRepository _folderApiRepository;
public FolderService(
IFolderRepository folderRepository,
IAuthService authService,
IFolderApiRepository folderApiRepository)
{
_folderRepository = folderRepository;
_authService = authService;
_folderApiRepository = folderApiRepository;
}
public async Task<Folder> GetByIdAsync(string id)
{
var data = await _folderRepository.GetByIdAsync(id);
if(data == null || data.UserId != _authService.UserId)
{
return null;
}
var folder = new Folder(data);
return folder;
}
public async Task<IEnumerable<Folder>> GetAllAsync()
{
var data = await _folderRepository.GetAllByUserIdAsync(_authService.UserId);
var folders = data.Select(f => new Folder(f));
return folders;
}
public async Task<ApiResult<FolderResponse>> SaveAsync(Folder folder)
{
ApiResult<FolderResponse> response = null;
var request = new FolderRequest(folder);
if(folder.Id == null)
{
response = await _folderApiRepository.PostAsync(request);
}
else
{
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);
}
}
return response;
}
public async Task<ApiResult<object>> DeleteAsync(string folderId)
{
ApiResult<object> response = await _folderApiRepository.DeleteAsync(folderId);
if(response.Succeeded)
{
await _folderRepository.DeleteAsync(folderId);
}
return response;
}
}
}