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

59 lines
1.6 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;
using Bit.App.Models.Data;
namespace Bit.App.Services
{
public class SiteService : Repository<SiteData, int>, ISiteService
{
2016-05-03 00:50:16 +03:00
private readonly IAuthService _authService;
private readonly IFolderService _folderService;
2016-05-02 09:52:09 +03:00
2016-05-03 00:50:16 +03:00
public SiteService(
ISqlService sqlService,
IAuthService authService,
IFolderService folderService)
2016-05-03 00:50:16 +03:00
: base(sqlService)
2016-05-02 09:52:09 +03:00
{
2016-05-03 00:50:16 +03:00
_authService = authService;
_folderService = folderService;
2016-05-03 00:50:16 +03:00
}
public new Task<IEnumerable<Site>> GetAllAsync()
{
var data = Connection.Table<SiteData>().Where(f => f.UserId == _authService.UserId).Cast<SiteData>();
return Task.FromResult(data.Select(s => new Site(s)));
2016-05-02 09:52:09 +03:00
}
public async Task SaveAsync(Site site)
{
2016-05-03 00:50:16 +03:00
var data = new SiteData(site, _authService.UserId);
2016-05-02 09:52:09 +03:00
data.RevisionDateTime = DateTime.UtcNow;
if(site.FolderId.HasValue && site.ServerFolderId == null)
{
var folder = await _folderService.GetByIdAsync(site.FolderId.Value);
if(folder != null)
{
site.ServerFolderId = folder.ServerId;
}
}
2016-05-02 09:52:09 +03:00
if(site.Id == 0)
{
await CreateAsync(data);
}
else
{
await ReplaceAsync(data);
}
site.Id = data.Id;
}
}
}