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

108 lines
3.3 KiB
C#
Raw Normal View History

2016-05-02 09:52:09 +03:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bit.App.Abstractions;
using Bit.App.Models;
2016-05-03 09:08:50 +03:00
using Bit.App.Models.Api;
2016-05-02 09:52:09 +03:00
using Bit.App.Models.Data;
using Xamarin.Forms;
2016-05-02 09:52:09 +03:00
namespace Bit.App.Services
{
public class SiteService : ISiteService
2016-05-02 09:52:09 +03:00
{
private readonly ISiteRepository _siteRepository;
2016-05-03 00:50:16 +03:00
private readonly IAuthService _authService;
private readonly ISiteApiRepository _siteApiRepository;
2016-05-02 09:52:09 +03:00
2016-05-03 00:50:16 +03:00
public SiteService(
ISiteRepository siteRepository,
IAuthService authService,
ISiteApiRepository siteApiRepository)
2016-05-02 09:52:09 +03:00
{
_siteRepository = siteRepository;
2016-05-03 00:50:16 +03:00
_authService = authService;
_siteApiRepository = siteApiRepository;
2016-05-03 00:50:16 +03:00
}
public async Task<Site> GetByIdAsync(string id)
2016-05-03 00:50:16 +03:00
{
var data = await _siteRepository.GetByIdAsync(id);
if(data == null || data.UserId != _authService.UserId)
{
return null;
}
var site = new Site(data);
return site;
}
public async Task<IEnumerable<Site>> GetAllAsync()
{
var data = await _siteRepository.GetAllByUserIdAsync(_authService.UserId);
var sites = data.Select(f => new Site(f));
return sites;
2016-05-02 09:52:09 +03:00
}
public async Task<IEnumerable<Site>> GetAllAsync(bool favorites)
{
var data = await _siteRepository.GetAllByUserIdAsync(_authService.UserId, favorites);
var sites = data.Select(f => new Site(f));
return sites;
}
2016-05-03 09:08:50 +03:00
public async Task<ApiResult<SiteResponse>> SaveAsync(Site site)
2016-05-02 09:52:09 +03:00
{
ApiResult<SiteResponse> response = null;
2016-05-03 09:08:50 +03:00
var request = new SiteRequest(site);
if(site.Id == null)
2016-05-02 09:52:09 +03:00
{
response = await _siteApiRepository.PostAsync(request);
2016-05-02 09:52:09 +03:00
}
else
{
response = await _siteApiRepository.PutAsync(site.Id, request);
}
if(response.Succeeded)
{
var data = new SiteData(response.Result, _authService.UserId);
if(site.Id == null)
{
await _siteRepository.InsertAsync(data);
site.Id = data.Id;
}
else
{
await _siteRepository.UpdateAsync(data);
}
2016-05-02 09:52:09 +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
return response;
2016-05-02 09:52:09 +03:00
}
2016-05-04 02:49:49 +03:00
public async Task<ApiResult> DeleteAsync(string id)
2016-05-04 02:49:49 +03:00
{
var response = await _siteApiRepository.DeleteAsync(id);
if(response.Succeeded)
2016-05-04 02:49:49 +03:00
{
await _siteRepository.DeleteAsync(id);
2016-05-04 02:49:49 +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-04 02:49:49 +03:00
return response;
2016-05-04 02:49:49 +03:00
}
2016-05-02 09:52:09 +03:00
}
}