public string SaveItem(AbstractItemViewModel item) { if (item.GetType() == GetType()) { var stringItem = (StringItemViewModel)item; var result = string.Empty; using (var db = new SQLiteConnection(DatabaseHandler.DBPath)) { try { var existingItem = (db.Table <StringItemModel>().Where(c => c.Id == item.ID)).SingleOrDefault(); if (existingItem != null) { existingItem.Id = stringItem.ID; existingItem.Name = stringItem.Name; existingItem.IsEncrypted = stringItem.IsEncrypted; existingItem.Content = stringItem.Content; existingItem.Nonce = CryptographicBuffer.EncodeToBase64String(stringItem.Nonce); existingItem.AuthenticationTag = CryptographicBuffer.EncodeToBase64String(stringItem.AuthenticationTag); db.Update(existingItem); result = "Existing Item detected - Item updated"; } else { db.Insert(new StringItemModel { IsEncrypted = stringItem.IsEncrypted, Id = stringItem.ID, Name = stringItem.Name, Content = stringItem.Content, Nonce = CryptographicBuffer.EncodeToBase64String(stringItem.Nonce), AuthenticationTag = CryptographicBuffer.EncodeToBase64String(stringItem.AuthenticationTag) }); result = "Success - new item added to database."; } } catch { result = "Failure - Item not saved"; } return(result); } } return("Failure Item not of correct class"); }
public string SaveItem(AbstractItemViewModel item) { if (item.GetType() == GetType()) { var fileItem = (FileItemViewModel)item; using (var db = new SQLiteConnection(DatabaseHandler.DBPath)) { string result; try { var existingItem = (db.Table <FileItemModel>().Where(c => c.Name == fileItem.Name)).SingleOrDefault(); if (existingItem != null) { existingItem.Id = fileItem.ID; existingItem.Name = fileItem.Name; existingItem.IsEncrypted = fileItem.IsEncrypted; existingItem.MruToken = fileItem.MruToken; existingItem.Nonce = CryptographicBuffer.EncodeToBase64String(fileItem.Nonce); db.Update(existingItem); result = "Existing Item detected - Item updated"; } else { db.Insert(new FileItemModel { IsEncrypted = fileItem.IsEncrypted, Id = fileItem.ID, Name = fileItem.Name, FileType = fileItem.Filetype.ToString(), MruToken = fileItem.MruToken, Nonce = CryptographicBuffer.EncodeToBase64String(fileItem.Nonce) }); result = "Success - new item added to database."; } } catch (Exception e) { result = "Failure - Item not saved"; } return(result); } } return("Failure - Item not of correct class"); }