Skip to content

Commit

Permalink
(#432) posts: core domain post class update, update events domain
Browse files Browse the repository at this point in the history
  • Loading branch information
SaintAngeLs committed Sep 24, 2024
1 parent a7bb3cf commit 0ecd203
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace MiniSpace.Services.Posts.Core.Entities
{
public class Post : AggregateRoot
{
public Guid? UserId { get; private set; }
public Guid? UserId { get; private set; }
public Guid? OrganizationId { get; private set; }
public Guid? EventId { get; private set; }
public string TextContent { get; private set; }
Expand All @@ -23,14 +23,19 @@ public class Post : AggregateRoot
public string Title { get; private set; }

// New fields to track the page ownership
public Guid? PageOwnerId { get; private set; } // Owner of the page where the post is published (could be User or Organization)
public PageOwnerType PageOwnerType { get; private set; } // Specifies if the page belongs to a user or an organization
public Guid? PageOwnerId { get; private set; } // Owner of the page where the post is published (could be User or Organization)
public PageOwnerType PageOwnerType { get; private set; } // Specifies if the page belongs to a user or an organization

// New fields to track reposts
public Guid? OriginalPostId { get; private set; }
public bool IsRepost => OriginalPostId.HasValue;

// Constructor
public Post(Guid id, Guid? userId, Guid? organizationId, Guid? eventId, string textContent,
IEnumerable<string> mediaFiles, DateTime createdAt, State state, PostContext context, DateTime? publishDate,
PostType type, string title = null, VisibilityStatus visibility = VisibilityStatus.Visible,
DateTime? updatedAt = null, Guid? pageOwnerId = null, PageOwnerType pageOwnerType = PageOwnerType.User)
DateTime? updatedAt = null, Guid? pageOwnerId = null, PageOwnerType pageOwnerType = PageOwnerType.User,
Guid? originalPostId = null)
{
Id = id;
UserId = userId;
Expand All @@ -48,6 +53,7 @@ public Post(Guid id, Guid? userId, Guid? organizationId, Guid? eventId, string t
Visibility = visibility;
PageOwnerId = pageOwnerId;
PageOwnerType = pageOwnerType;
OriginalPostId = originalPostId;

AddEvent(new PostCreatedEvent(Id));
}
Expand Down Expand Up @@ -94,6 +100,20 @@ public static Post CreateEventPost(Guid id, Guid userId, Guid organizationId, Gu
publishDate ?? createdAt, PostType.SocialPost, title: null, visibility, updatedAt: null, pageOwnerId: organizationId, pageOwnerType: PageOwnerType.Organization);
}

// Factory method for repost
public static Post CreateRepost(Guid id, Guid userId, Post originalPost, DateTime createdAt, State state)
{
if (originalPost == null)
{
throw new InvalidPostStateException("Original post cannot be null.");
}

return new Post(id, userId, null, originalPost.EventId, originalPost.TextContent, originalPost.MediaFiles,
createdAt, state, originalPost.Context, publishDate: createdAt, originalPost.Type,
originalPost.Title, originalPost.Visibility, pageOwnerId: userId, pageOwnerType: PageOwnerType.User,
originalPostId: originalPost.Id);
}

// Methods to manage post state

public void SetToBePublished(DateTime publishDate, DateTime now)
Expand Down Expand Up @@ -195,6 +215,17 @@ public void RemoveMediaFile(string mediaFileUrl, DateTime now)
UpdatedAt = now;
}

// Repost the post
public void Repost(Guid userId, DateTime now)
{
if (IsRepost)
{
throw new InvalidPostStateException("Cannot repost a repost.");
}

AddEvent(new PostRepostedEvent(Id, userId, OriginalPostId ?? Id, now));
}

// Validation Methods
private static void CheckTextContent(AggregateId id, string textContent, PostType postType)
{
Expand All @@ -215,3 +246,4 @@ private static void CheckPublishDate(AggregateId id, State state, DateTime publi
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace MiniSpace.Services.Posts.Core.Events
{
public class PostRepostedEvent : IDomainEvent
{
public Guid PostId { get; }
public Guid UserId { get; }
public Guid OriginalPostId { get; }
public DateTime RepostedAt { get; }

public PostRepostedEvent(Guid postId, Guid userId, Guid originalPostId, DateTime repostedAt)
{
PostId = postId;
UserId = userId;
OriginalPostId = originalPostId;
RepostedAt = repostedAt;
}
}
}

0 comments on commit 0ecd203

Please sign in to comment.