-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordEntry.cs
56 lines (48 loc) · 1.93 KB
/
WordEntry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using VkCore.Events.Word;
using VkCore.Extensions;
using VkCore.Interfaces;
using VkCore.SharedKernel;
namespace VkCore.Models.Word
{
public class WordEntry : BaseEntity, IResponseErrorLogger
{
// Use uninitialised backing fields - this means we can detect if the collection was loaded
// don't listen to resharper, these should not be readonly as then EF can't write to them
private HashSet<Annotation> _annotations;
private List<string> _errors = new List<string>();
public string Word { get; set; }
public IEnumerable<Annotation> Annotations => _annotations?.ToList();
public IEnumerable<StudentWord> Students { get; set; }
private WordEntry() { }
public WordEntry(string word)
{
Id = Guid.NewGuid().ToString();
var cleanWord = word.RemoveNonAlphaNumeric();
Word = cleanWord;
_annotations = new HashSet<Annotation>();
AddEvent(new WordAddedEvent(cleanWord));
}
public void AddAnnotation(Annotation newAnnotation, DbContext dbContext = null
) {
if (_annotations == null)
{
if (dbContext == null)
{
throw new ArgumentNullException(nameof(dbContext),
$"You must provide a context if the annotation collection isn't valid.");
}
dbContext.Entry(this).Collection(o => o.Annotations).Load();
}
if (newAnnotation.IsUserCreated() && string.IsNullOrEmpty(newAnnotation.UpdatedBy))
{
throw new ArgumentException("If an annotation is user created, the UpdatedBy property must be set.");
}
_annotations.Add(newAnnotation);
}
public IEnumerable<string> Errors { get; }
}
}