From 216c96a02f94f6ea00581424a715ee798142d844 Mon Sep 17 00:00:00 2001 From: Thomas Rittson Date: Wed, 30 Jun 2021 09:28:41 +1000 Subject: [PATCH] Improve and refactor style, fix typo --- src/Api/Controllers/AccountsController.cs | 37 +++++++------------ .../SqlServer/CipherRepository.cs | 2 +- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/Api/Controllers/AccountsController.cs b/src/Api/Controllers/AccountsController.cs index 0ec437b114..298a82a28b 100644 --- a/src/Api/Controllers/AccountsController.cs +++ b/src/Api/Controllers/AccountsController.cs @@ -285,40 +285,31 @@ namespace Bit.Api.Controllers throw new UnauthorizedAccessException(); } - var existingCiphers = await _cipherRepository.GetManyByUserIdAsync(user.Id); - var ciphersDict = model.Ciphers?.ToDictionary(c => c.Id.Value); var ciphers = new List(); - if (existingCiphers.Any() && ciphersDict != null) + if (model.Ciphers.Any()) { - foreach (var cipher in existingCiphers.Where(c => ciphersDict.ContainsKey(c.Id))) - { - ciphers.Add(ciphersDict[cipher.Id].ToCipher(cipher)); - } + var existingCiphers = await _cipherRepository.GetManyByUserIdAsync(user.Id); + ciphers = existingCiphers + .Join(model.Ciphers, c => c.Id, c => c.Id, (existing, c) => c.ToCipher(existing)) + .ToList(); } - var existingFolders = await _folderRepository.GetManyByUserIdAsync(user.Id); - var foldersDict = model.Folders?.ToDictionary(f => f.Id); var folders = new List(); - if (existingFolders.Any() && foldersDict != null) + if (model.Folders.Any()) { - foreach (var folder in existingFolders.Where(f => foldersDict.ContainsKey(f.Id))) - { - folders.Add(foldersDict[folder.Id].ToFolder(folder)); - } + var existingFolders = await _folderRepository.GetManyByUserIdAsync(user.Id); + folders = existingFolders + .Join(model.Folders, f => f.Id, f => f.Id, (existing, f) => f.ToFolder(existing)) + .ToList(); } var sends = new List(); - if (model.Sends?.Count() > 0) + if (model.Sends?.Any() == true) { var existingSends = await _sendRepository.GetManyByUserIdAsync(user.Id); - var sendsDict = model.Sends?.ToDictionary(s => s.Id); - if (existingSends.Any() && sendsDict != null) - { - foreach (var send in existingSends.Where(s => sendsDict.ContainsKey(s.Id))) - { - sends.Add(sendsDict[send.Id].ToSend(send, _sendService)); - } - } + sends = existingSends + .Join(model.Sends, s => s.Id, s => s.Id, (existing, s) => s.ToSend(existing, _sendService)) + .ToList(); } var result = await _userService.UpdateKeyAsync( diff --git a/src/Core/Repositories/SqlServer/CipherRepository.cs b/src/Core/Repositories/SqlServer/CipherRepository.cs index faf626d768..ed7f65757c 100644 --- a/src/Core/Repositories/SqlServer/CipherRepository.cs +++ b/src/Core/Repositories/SqlServer/CipherRepository.cs @@ -869,7 +869,7 @@ namespace Bit.Core.Repositories.SqlServer var s = sends.FirstOrDefault(); if (s == null) { - throw new ApplicationException("Must have some ciphers to bulk import."); + throw new ApplicationException("Must have some Sends to bulk import."); } var sendsTable = new DataTable("SendsDataTable");