/// <summary> /// Konstruktor för ett giltigt Post-objekt /// </summary> /// <param name="createdByID">UserID för den User som skapat posten</param> /// <param name="title">Titel för posten</param> /// <param name="body">Body/Meddelande för posten</param> /// <param name="parent">[Optional, Default: null] Den överliggande posten</param> public Post(Guid createdByID, string title, string body, Post parent = null) { PostID = Guid.NewGuid(); CreatedByID = createdByID; Title = title; Body = body; ParentPost = parent; CreateDate = DateTime.UtcNow; }
/// <summary> /// Läger till ett Post-objekt i repositoriet /// </summary> /// <param name="newPost">Det Post-objekt som skall läggas till</param> public void AddPost(Post newPost) { if (newPost.PostID == null) throw new Exception("PostID is not allowed to be null!"); if (newPost.CreatedByID == null) throw new Exception("CreatedByID is not allowed to be null!"); if (!users.Any(u => u.UserID == newPost.CreatedByID)) throw new Exception("CreatedByID does not match any user!"); if (string.IsNullOrEmpty(newPost.Body)) throw new Exception("Body is not allowed to be empty!"); if (newPost.CreateDate == null || newPost.CreateDate == new DateTime()) throw new Exception("CreateDate must be set"); if (posts.Any(p => p.PostID == newPost.PostID)) throw new Exception("A Post with that PostID already Exists!"); posts.Add(newPost); }
// Genererar en Post med slumpade fält. private static Post GenerateSinglePost(Random rng, List<string> postTitles, List<string> postBodies, List<Guid> UserIDs) { int userIndex = rng.Next(UserIDs.Count - 1); int postTitleIndex = rng.Next(postTitles.Count - 1); int postBodyIndex = rng.Next(postBodies.Count - 1); int dayOffset = -1 * rng.Next(60); int hourOffset = -1 * rng.Next(23); int minuteOffset = -1 * rng.Next(59); List<Post.PostTags> tagList = GenerateTagList(rng); Post newPost = new Post { CreatedByID = UserIDs[userIndex], Title = postTitles[postTitleIndex], Body = postBodies[postBodyIndex], CreateDate = DateTime.Now.AddDays(dayOffset).AddHours(hourOffset).AddMinutes(minuteOffset), Tags = tagList }; return newPost; }