Files
server/src/Core/Models/Api/Response/CipherResponseModel.cs

130 lines
4.0 KiB
C#
Raw Normal View History

using System;
using Core.Models.Data;
using System.Collections.Generic;
using Bit.Core.Models.Table;
using System.Linq;
2017-03-08 21:55:08 -05:00
namespace Bit.Core.Models.Api
{
2017-04-17 17:01:23 -04:00
public class CipherMiniResponseModel : ResponseModel
{
2017-04-17 17:01:23 -04:00
public CipherMiniResponseModel(Cipher cipher, string obj = "cipherMini")
: base(obj)
{
if(cipher == null)
{
throw new ArgumentNullException(nameof(cipher));
}
Id = cipher.Id.ToString();
Type = cipher.Type;
RevisionDate = cipher.RevisionDate;
OrganizationId = cipher.OrganizationId?.ToString();
switch(cipher.Type)
{
case Enums.CipherType.Login:
2017-01-02 21:52:13 -05:00
Data = new LoginDataModel(cipher);
break;
default:
throw new ArgumentException("Unsupported " + nameof(Type) + ".");
}
}
[Obsolete]
public CipherMiniResponseModel(Folder folder, string obj = "cipherMini")
: base(obj)
{
if(folder == null)
{
throw new ArgumentNullException(nameof(folder));
}
Id = folder.Id.ToString();
Type = Enums.CipherType.Folder;
RevisionDate = folder.RevisionDate;
Data = new FolderDataModel(folder);
}
public string Id { get; set; }
public string OrganizationId { get; set; }
public Enums.CipherType Type { get; set; }
public dynamic Data { get; set; }
public DateTime RevisionDate { get; set; }
}
2017-04-17 17:01:23 -04:00
public class CipherResponseModel : CipherMiniResponseModel
{
public CipherResponseModel(CipherDetails cipher, string obj = "cipher")
: base(cipher, obj)
{
FolderId = cipher.FolderId?.ToString();
Favorite = cipher.Favorite;
}
[Obsolete]
public CipherResponseModel(Folder folder, string obj = "cipher")
: base(folder, obj)
{ }
2017-04-17 17:01:23 -04:00
public string FolderId { get; set; }
public bool Favorite { get; set; }
}
public class CipherDetailsResponseModel : CipherResponseModel
{
public CipherDetailsResponseModel(CipherDetails cipher,
2017-04-27 09:19:30 -04:00
IDictionary<Guid, IGrouping<Guid, CollectionCipher>> collectionCiphers, string obj = "cipherDetails")
2017-04-04 17:22:47 -04:00
: base(cipher, obj)
{
2017-04-27 09:19:30 -04:00
if(collectionCiphers.ContainsKey(cipher.Id))
{
2017-04-27 09:39:21 -04:00
CollectionIds = collectionCiphers[cipher.Id].Select(c => c.CollectionId);
}
else
{
2017-04-27 09:19:30 -04:00
CollectionIds = new Guid[] { };
}
}
2017-04-27 09:19:30 -04:00
public CipherDetailsResponseModel(CipherDetails cipher, IEnumerable<CollectionCipher> collectionCiphers,
2017-04-04 17:22:47 -04:00
string obj = "cipherDetails")
: base(cipher, obj)
{
2017-04-27 09:39:21 -04:00
CollectionIds = collectionCiphers.Select(c => c.CollectionId);
2017-04-04 17:22:47 -04:00
}
2017-04-27 09:19:30 -04:00
public IEnumerable<Guid> CollectionIds { get; set; }
}
2017-04-04 17:22:47 -04:00
2017-04-17 17:01:23 -04:00
public class CipherMiniDetailsResponseModel : CipherMiniResponseModel
{
public CipherMiniDetailsResponseModel(Cipher cipher,
2017-04-27 09:19:30 -04:00
IDictionary<Guid, IGrouping<Guid, CollectionCipher>> collectionCiphers, string obj = "cipherMiniDetails")
2017-04-17 17:01:23 -04:00
: base(cipher, obj)
{
2017-04-27 09:19:30 -04:00
if(collectionCiphers.ContainsKey(cipher.Id))
2017-04-17 17:01:23 -04:00
{
2017-04-27 09:39:21 -04:00
CollectionIds = collectionCiphers[cipher.Id].Select(c => c.CollectionId);
2017-04-17 17:01:23 -04:00
}
else
{
2017-04-27 09:19:30 -04:00
CollectionIds = new Guid[] { };
2017-04-17 17:01:23 -04:00
}
}
2017-04-27 09:19:30 -04:00
public IEnumerable<Guid> CollectionIds { get; set; }
2017-04-17 17:01:23 -04:00
}
2017-04-04 17:22:47 -04:00
public class CipherFullDetailsResponseModel : CipherDetailsResponseModel
{
2017-04-27 09:19:30 -04:00
public CipherFullDetailsResponseModel(CipherFullDetails cipher, IEnumerable<CollectionCipher> collectionCiphers)
: base(cipher, collectionCiphers, "cipherFullDetails")
2017-04-04 17:22:47 -04:00
{
Edit = cipher.Edit;
}
public bool Edit { get; set; }
}
}