public void Add(ITextModel model) { string name = model.GetName(); if (_db.Texts.Any(x => x.Name == name)) { throw new ArgumentException($"Cannot add model since a model already exists in the database with name {model.GetName()}."); } WordCount withQuotes = TranslateCounts(model.GetCountsWithQuotes()); _db.WordCounts.Add(withQuotes); _db.SaveChanges(); WordCount withoutQuotes = TranslateCounts(model.GetCountsWithoutQuotes()); _db.WordCounts.Add(withoutQuotes); _db.SaveChanges(); Text text = new Text() { Name = model.GetName(), Author = model.GetAuthor(), WithQuotesId = withQuotes.Id, WithoutQuotesId = withoutQuotes.Id, IncludeQuotes = 1 }; _db.Texts.Add(text); _db.SaveChanges(); }
public void Delete(ITextModel model) { string name = model.GetName(); Text text = _db.Texts.FirstOrDefault(x => x.Name == name); if (text == null) { throw new ArgumentException($"Cannot delete text {model.GetName()} since it does not exist in the database."); } Disassociate(text); _db.Texts.Remove(text); _db.SaveChanges(); }
public void ModifyIncludeQuotes(ITextModel model, bool includeQuotes) { string name = model.GetName(); Text text = _db.Texts.FirstOrDefault(x => x.Name == name); if (text == null) { throw new ArgumentException("Cannot update model since no corresponding text exists in the database."); } text.IncludeQuotes = Convert.ToInt32(includeQuotes); _db.SaveChanges(); }
public void ModifyAuthor(ITextModel model, string newAuthor) { string name = model.GetName(); Text text = _db.Texts.FirstOrDefault(x => x.Name == name); if (text == null) { throw new ArgumentException("Cannot update model since no corresponding text exists in the database."); } if (string.IsNullOrWhiteSpace(newAuthor)) { text.Author = null; } else { text.Author = newAuthor; } _db.SaveChanges(); }
public void ModifyName(ITextModel model, string newName) { string name = model.GetName(); Text text = _db.Texts.FirstOrDefault(x => x.Name == name); if (text == null) { throw new ArgumentException("Cannot update model since no corresponding text exists in the database."); } if (string.IsNullOrWhiteSpace(newName)) { throw new ArgumentException("Cannot update model because new name is null or white space."); } if (_db.Texts.Any(x => x.Name == newName)) { throw new ArgumentException($"Cannot update model since a text already exists with the name {newName}"); } text.Name = newName; _db.SaveChanges(); }
private void CompareTextModels(ITextModel model1, ITextModel model2) { Assert.AreEqual(model1.GetName(), model2.GetName()); Assert.AreEqual(model1.GetAuthor(), model2.GetAuthor()); Assert.AreEqual(model1.GetIncludeQuotes(), model2.GetIncludeQuotes()); ISingleWordCountModel counts1 = model1.GetCounts(); ISingleWordCountModel counts2 = model1.GetCounts(); ISingleWordCountModel withQuotes1 = model1.GetCountsWithQuotes(); ISingleWordCountModel withQuotes2 = model2.GetCountsWithQuotes(); ISingleWordCountModel withoutQuotes1 = model1.GetCountsWithoutQuotes(); ISingleWordCountModel withoutQuotes2 = model2.GetCountsWithoutQuotes(); for (int i = 0; i < withQuotes1.GetLength(); i++) { Assert.AreEqual(counts1.GetAt(i), counts2.GetAt(i)); Assert.AreEqual(withQuotes1.GetAt(i), withQuotes2.GetAt(i)); Assert.AreEqual(withoutQuotes1.GetAt(i), withoutQuotes2.GetAt(i)); } Assert.AreEqual(model1.GetLength(), model2.GetLength()); }
public void ValidSetName() { model.SetName("Gary"); Assert.AreEqual("Gary", model.GetName()); }