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

79 lines
2.3 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;
2016-05-02 09:52:09 +03:00
namespace Bit.App.Services
{
public class FolderService : IFolderService
2016-05-02 09:52:09 +03:00
{
private readonly IFolderRepository _folderRepository;
2016-05-03 00:50:16 +03:00
private readonly IAuthService _authService;
private readonly IFolderApiRepository _folderApiRepository;
2016-05-02 09:52:09 +03:00
2016-05-03 00:50:16 +03:00
public FolderService(
IFolderRepository folderRepository,
2016-05-03 09:08:50 +03:00
IAuthService authService,
IFolderApiRepository folderApiRepository)
2016-05-02 09:52:09 +03:00
{
_folderRepository = folderRepository;
2016-05-03 00:50:16 +03:00
_authService = authService;
_folderApiRepository = folderApiRepository;
2016-05-03 00:50:16 +03:00
}
public async Task<Folder> GetByIdAsync(string id)
{
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);
return folder;
}
public async Task<IEnumerable<Folder>> GetAllAsync()
2016-05-03 00:50:16 +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));
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
{
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
{
response = await _folderApiRepository.PostAsync(request);
2016-05-02 09:52:09 +03:00
}
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);
}
2016-05-02 09:52:09 +03:00
}
return response;
2016-05-02 09:52:09 +03:00
}
}
}