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;
|
2016-05-03 01:35:01 +03:00
|
|
|
|
private readonly IFolderService _folderService;
|
2016-05-02 09:52:09 +03:00
|
|
|
|
|
2016-05-03 00:50:16 +03:00
|
|
|
|
public SiteService(
|
|
|
|
|
ISqlService sqlService,
|
2016-05-03 01:35:01 +03:00
|
|
|
|
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;
|
2016-05-03 01:35:01 +03:00
|
|
|
|
_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;
|
|
|
|
|
|
2016-05-03 01:35:01 +03:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|