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;
|
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 SiteService : ISiteService
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-05-06 07:17:38 +03:00
|
|
|
|
private readonly ISiteRepository _siteRepository;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
private readonly IAuthService _authService;
|
2016-05-06 07:17:38 +03:00
|
|
|
|
private readonly ISiteApiRepository _siteApiRepository;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-05-03 00:50:16 +03:00
|
|
|
|
public SiteService(
|
2016-05-06 07:17:38 +03:00
|
|
|
|
ISiteRepository siteRepository,
|
2016-05-03 01:35:01 +03:00
|
|
|
|
IAuthService authService,
|
2016-05-06 07:17:38 +03:00
|
|
|
|
ISiteApiRepository siteApiRepository)
|
2016-05-02 09:52:09 +03:00
|
|
|
|
{
|
2016-05-06 07:17:38 +03:00
|
|
|
|
_siteRepository = siteRepository;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
_authService = authService;
|
2016-05-06 07:17:38 +03:00
|
|
|
|
_siteApiRepository = siteApiRepository;
|
2016-05-03 00:50:16 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 07:17:38 +03:00
|
|
|
|
public async Task<Site> GetByIdAsync(string id)
|
2016-05-03 00:50:16 +03:00
|
|
|
|
{
|
2016-05-06 07:17:38 +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
|
|
|
|
}
|
|
|
|
|
|
2016-06-15 06:23:05 +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
|
|
|
|
{
|
2016-05-06 07:17:38 +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
|
|
|
|
{
|
2016-05-06 07:17:38 +03:00
|
|
|
|
response = await _siteApiRepository.PostAsync(request);
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-05-06 07:17:38 +03:00
|
|
|
|
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
|
|
|
|
}
|
2016-07-20 01:46:39 +03:00
|
|
|
|
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|
|
|
|
|
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
|
|
|
|
|
{
|
2016-07-20 02:38:13 +03:00
|
|
|
|
Device.BeginInvokeOnMainThread(() => MessagingCenter.Send(Application.Current, "Logout", (string)null));
|
2016-07-20 01:46:39 +03:00
|
|
|
|
}
|
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-04 02:49:49 +03:00
|
|
|
|
|
2016-07-02 02:06:07 +03:00
|
|
|
|
public async Task<ApiResult> DeleteAsync(string id)
|
2016-05-04 02:49:49 +03:00
|
|
|
|
{
|
2016-07-02 02:06:07 +03:00
|
|
|
|
var response = await _siteApiRepository.DeleteAsync(id);
|
2016-05-06 07:17:38 +03:00
|
|
|
|
if(response.Succeeded)
|
2016-05-04 02:49:49 +03:00
|
|
|
|
{
|
2016-05-06 07:17:38 +03:00
|
|
|
|
await _siteRepository.DeleteAsync(id);
|
2016-05-04 02:49:49 +03:00
|
|
|
|
}
|
2016-07-20 01:46:39 +03:00
|
|
|
|
else if(response.StatusCode == System.Net.HttpStatusCode.Forbidden
|
|
|
|
|
|| response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
|
|
|
|
|
{
|
2016-07-20 02:38:13 +03:00
|
|
|
|
Device.BeginInvokeOnMainThread(() => MessagingCenter.Send(Application.Current, "Logout", (string)null));
|
2016-07-20 01:46:39 +03:00
|
|
|
|
}
|
2016-05-04 02:49:49 +03:00
|
|
|
|
|
2016-05-06 07:17:38 +03:00
|
|
|
|
return response;
|
2016-05-04 02:49:49 +03:00
|
|
|
|
}
|
2016-05-02 09:52:09 +03:00
|
|
|
|
}
|
|
|
|
|
}
|