mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 16:42:50 -05:00
Folder APIs to new tables
This commit is contained in:
@ -1,35 +0,0 @@
|
||||
using System;
|
||||
using Bit.Core.Models.Table;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Bit.Core.Models.Api
|
||||
{
|
||||
public class FolderDataModel
|
||||
{
|
||||
public FolderDataModel() { }
|
||||
|
||||
public FolderDataModel(FolderRequestModel folder)
|
||||
{
|
||||
Name = folder.Name;
|
||||
}
|
||||
|
||||
public FolderDataModel(CipherRequestModel cipher)
|
||||
{
|
||||
Name = cipher.Name;
|
||||
}
|
||||
|
||||
public FolderDataModel(Cipher cipher)
|
||||
{
|
||||
if(cipher.Type != Core.Enums.CipherType.Folder)
|
||||
{
|
||||
throw new ArgumentException("Cipher is not correct type.");
|
||||
}
|
||||
|
||||
var data = JsonConvert.DeserializeObject<FolderDataModel>(cipher.Data);
|
||||
|
||||
Name = data.Name;
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
@ -13,19 +13,17 @@ namespace Bit.Core.Models.Api
|
||||
[StringLength(300)]
|
||||
public string Name { get; set; }
|
||||
|
||||
public Cipher ToCipher(Guid userId)
|
||||
public Folder ToFolder(Guid userId)
|
||||
{
|
||||
return ToCipher(new Cipher
|
||||
return ToFolder(new Folder
|
||||
{
|
||||
UserId = userId
|
||||
});
|
||||
}
|
||||
|
||||
public Cipher ToCipher(Cipher existingFolder)
|
||||
public Folder ToFolder(Folder existingFolder)
|
||||
{
|
||||
existingFolder.Data = JsonConvert.SerializeObject(new FolderDataModel(this), new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
|
||||
existingFolder.Type = Core.Enums.CipherType.Folder;
|
||||
|
||||
existingFolder.Name = Name;
|
||||
return existingFolder;
|
||||
}
|
||||
}
|
||||
|
@ -5,24 +5,17 @@ namespace Bit.Core.Models.Api
|
||||
{
|
||||
public class FolderResponseModel : ResponseModel
|
||||
{
|
||||
public FolderResponseModel(Cipher cipher, Guid userId)
|
||||
public FolderResponseModel(Folder folder)
|
||||
: base("folder")
|
||||
{
|
||||
if(cipher == null)
|
||||
if(folder == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(cipher));
|
||||
throw new ArgumentNullException(nameof(folder));
|
||||
}
|
||||
|
||||
if(cipher.Type != Core.Enums.CipherType.Folder)
|
||||
{
|
||||
throw new ArgumentException(nameof(cipher.Type));
|
||||
}
|
||||
|
||||
var data = new FolderDataModel(cipher);
|
||||
|
||||
Id = cipher.Id.ToString();
|
||||
Name = data.Name;
|
||||
RevisionDate = cipher.RevisionDate;
|
||||
Id = folder.Id.ToString();
|
||||
Name = folder.Name;
|
||||
RevisionDate = folder.RevisionDate;
|
||||
}
|
||||
|
||||
public string Id { get; set; }
|
||||
|
@ -1,9 +1,13 @@
|
||||
using System;
|
||||
using Bit.Core.Models.Table;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Bit.Core.Repositories
|
||||
{
|
||||
public interface IFolderRepository : IRepository<Folder, Guid>
|
||||
{
|
||||
Task<Folder> GetByIdAsync(Guid id, Guid userId);
|
||||
Task<ICollection<Folder>> GetManyByUserIdAsync(Guid userId);
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,11 @@
|
||||
using System;
|
||||
using Bit.Core.Models.Table;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using Dapper;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bit.Core.Repositories.SqlServer
|
||||
{
|
||||
@ -12,5 +18,29 @@ namespace Bit.Core.Repositories.SqlServer
|
||||
public FolderRepository(string connectionString)
|
||||
: base(connectionString)
|
||||
{ }
|
||||
|
||||
public async Task<Folder> GetByIdAsync(Guid id, Guid userId)
|
||||
{
|
||||
var folder = await GetByIdAsync(id);
|
||||
if(folder == null || folder.UserId != userId)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return folder;
|
||||
}
|
||||
|
||||
public async Task<ICollection<Folder>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.QueryAsync<Folder>(
|
||||
$"[{Schema}].[Folder_ReadByUserId]",
|
||||
new { UserId = userId },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
return results.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user