From 9c61cfb5c0430f2a19f3312c2aedf690c0b07335 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 4 May 2016 22:39:23 -0400 Subject: [PATCH] Added "since" revision date parameter to folder and site "get all" APIs. --- src/Api/Controllers/FoldersController.cs | 14 ++++++++++++-- src/Api/Controllers/SitesController.cs | 12 ++++++++++-- src/Core/Repositories/IFolderRepository.cs | 4 +++- src/Core/Repositories/ISiteRepository.cs | 1 + .../Repositories/SqlServer/FolderRepository.cs | 13 +++++++++++++ src/Core/Repositories/SqlServer/SiteRepository.cs | 13 +++++++++++++ src/Sql/Sql.sqlproj | 2 ++ .../Folder_ReadByRevisionDate.sql | 13 +++++++++++++ .../Stored Procedures/Site_ReadByRevisionDate.sql | 13 +++++++++++++ 9 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 src/Sql/dbo/Stored Procedures/Folder_ReadByRevisionDate.sql create mode 100644 src/Sql/dbo/Stored Procedures/Site_ReadByRevisionDate.sql diff --git a/src/Api/Controllers/FoldersController.cs b/src/Api/Controllers/FoldersController.cs index 934ecc064..a0183e43b 100644 --- a/src/Api/Controllers/FoldersController.cs +++ b/src/Api/Controllers/FoldersController.cs @@ -8,6 +8,7 @@ using System.Security.Claims; using Microsoft.AspNet.Authorization; using Bit.Api.Models; using Bit.Core.Exceptions; +using Bit.Core.Domains; namespace Bit.Api.Controllers { @@ -36,9 +37,18 @@ namespace Bit.Api.Controllers } [HttpGet("")] - public async Task> Get() + public async Task> Get(DateTime? since = null) { - var folders = await _folderRepository.GetManyByUserIdAsync(User.GetUserId()); + ICollection folders = null; + if(since.HasValue) + { + folders = await _folderRepository.GetManyByRevisionDateAsync(User.GetUserId(), since.Value); + } + else + { + folders = await _folderRepository.GetManyByUserIdAsync(User.GetUserId()); + } + return new ListResponseModel(folders.Select(f => new FolderResponseModel(f))); } diff --git a/src/Api/Controllers/SitesController.cs b/src/Api/Controllers/SitesController.cs index 746cb58c9..acab6434f 100644 --- a/src/Api/Controllers/SitesController.cs +++ b/src/Api/Controllers/SitesController.cs @@ -42,9 +42,17 @@ namespace Bit.Api.Controllers } [HttpGet("")] - public async Task> Get(string[] expand = null) + public async Task> Get(DateTime? since = null, string[] expand = null) { - var sites = await _siteRepository.GetManyByUserIdAsync(User.GetUserId()); + ICollection sites = null; + if(since.HasValue) + { + sites = await _siteRepository.GetManyByRevisionDateAsync(User.GetUserId(), since.Value); + } + else + { + sites = await _siteRepository.GetManyByUserIdAsync(User.GetUserId()); + } var responses = sites.Select(s => new SiteResponseModel(s)).ToList(); await ExpandManyAsync(sites, responses, expand, null); diff --git a/src/Core/Repositories/IFolderRepository.cs b/src/Core/Repositories/IFolderRepository.cs index 18fff1621..48d524046 100644 --- a/src/Core/Repositories/IFolderRepository.cs +++ b/src/Core/Repositories/IFolderRepository.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading.Tasks; using Bit.Core.Domains; @@ -8,5 +9,6 @@ namespace Bit.Core.Repositories { Task GetByIdAsync(string id, string userId); Task> GetManyByUserIdAsync(string userId); + Task> GetManyByRevisionDateAsync(string userId, DateTime sinceRevisionDate); } } diff --git a/src/Core/Repositories/ISiteRepository.cs b/src/Core/Repositories/ISiteRepository.cs index f9aeeba8a..7d081ba83 100644 --- a/src/Core/Repositories/ISiteRepository.cs +++ b/src/Core/Repositories/ISiteRepository.cs @@ -9,5 +9,6 @@ namespace Bit.Core.Repositories { Task GetByIdAsync(string id, string userId); Task> GetManyByUserIdAsync(string userId); + Task> GetManyByRevisionDateAsync(string userId, DateTime sinceRevisionDate); } } diff --git a/src/Core/Repositories/SqlServer/FolderRepository.cs b/src/Core/Repositories/SqlServer/FolderRepository.cs index fb1b9b908..e26495a6f 100644 --- a/src/Core/Repositories/SqlServer/FolderRepository.cs +++ b/src/Core/Repositories/SqlServer/FolderRepository.cs @@ -39,5 +39,18 @@ namespace Bit.Core.Repositories.SqlServer return results.Select(f => f.ToDomain()).ToList(); } } + + public async Task> GetManyByRevisionDateAsync(string userId, DateTime sinceRevisionDate) + { + using(var connection = new SqlConnection(ConnectionString)) + { + var results = await connection.QueryAsync( + $"[{Schema}].[{Table}_ReadByRevisionDate]", + new { UserId = new Guid(userId), SinceRevisionDate = sinceRevisionDate }, + commandType: CommandType.StoredProcedure); + + return results.Select(f => f.ToDomain()).ToList(); + } + } } } diff --git a/src/Core/Repositories/SqlServer/SiteRepository.cs b/src/Core/Repositories/SqlServer/SiteRepository.cs index 8ec07ba45..dc0b0e266 100644 --- a/src/Core/Repositories/SqlServer/SiteRepository.cs +++ b/src/Core/Repositories/SqlServer/SiteRepository.cs @@ -39,5 +39,18 @@ namespace Bit.Core.Repositories.SqlServer return results.Select(s => s.ToDomain()).ToList(); } } + + public async Task> GetManyByRevisionDateAsync(string userId, DateTime sinceRevisionDate) + { + using(var connection = new SqlConnection(ConnectionString)) + { + var results = await connection.QueryAsync( + $"[{Schema}].[{Table}_ReadByRevisionDate]", + new { UserId = new Guid(userId), SinceRevisionDate = sinceRevisionDate }, + commandType: CommandType.StoredProcedure); + + return results.Select(f => f.ToDomain()).ToList(); + } + } } } diff --git a/src/Sql/Sql.sqlproj b/src/Sql/Sql.sqlproj index 009c63fee..7e87ee146 100644 --- a/src/Sql/Sql.sqlproj +++ b/src/Sql/Sql.sqlproj @@ -88,5 +88,7 @@ + + \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Folder_ReadByRevisionDate.sql b/src/Sql/dbo/Stored Procedures/Folder_ReadByRevisionDate.sql new file mode 100644 index 000000000..724561ccf --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/Folder_ReadByRevisionDate.sql @@ -0,0 +1,13 @@ +CREATE PROCEDURE [dbo].[Folder_ReadByRevisionDate] + @UserId UNIQUEIDENTIFIER, + @SinceRevisionDate DATETIME +AS +BEGIN + SELECT + * + FROM + [dbo].[FolderView] + WHERE + [UserId] = @UserId + AND [RevisionDate] >= @SinceRevisionDate +END diff --git a/src/Sql/dbo/Stored Procedures/Site_ReadByRevisionDate.sql b/src/Sql/dbo/Stored Procedures/Site_ReadByRevisionDate.sql new file mode 100644 index 000000000..fe6bf12c9 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/Site_ReadByRevisionDate.sql @@ -0,0 +1,13 @@ +CREATE PROCEDURE [dbo].[Site_ReadByRevisionDate] + @UserId UNIQUEIDENTIFIER, + @SinceRevisionDate DATETIME +AS +BEGIN + SELECT + * + FROM + [dbo].[SiteView] + WHERE + [UserId] = @UserId + AND [RevisionDate] >= @SinceRevisionDate +END