Skip to content

Commit

Permalink
(#87) update organization to maintain tree structure
Browse files Browse the repository at this point in the history
  • Loading branch information
eggwhat committed Apr 26, 2024
1 parent c99bb18 commit 08a711a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public async Task HandleAsync(AddOrganization command, CancellationToken cancell
{
throw new ParentOrganizationNotFoundException(command.ParentId);
}
parent.MakeParent();
await _organizationRepository.UpdateAsync(parent);
}
await _organizationRepository.AddAsync(organization);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class OrganizationDto
public Guid Id { get; set; }
public string Name { get; set; }
public Guid ParentId { get; set; }
public int MemberCount { get; set; }
public bool IsLeaf { get; set; }
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ public class Organization : AggregateRoot
private ISet<Organizer> _organizers = new HashSet<Organizer>();
public string Name { get; private set; }
public Guid ParentId { get; private set; }
public bool IsRoot => ParentId == Guid.Empty;
public bool IsLeaf => ParentId != Guid.Empty;
public bool IsLeaf { get; private set; }

public IEnumerable<Organizer> Organizers
{
get => _organizers;
private set => _organizers = new HashSet<Organizer>(value);
}

public Organization(Guid id, string name, Guid parentId, IEnumerable<Organizer> organizers = null)
public Organization(Guid id, string name, Guid parentId, bool isLeaf = true, IEnumerable<Organizer> organizers = null)
{
Id = id;
Name = name;
ParentId = parentId;
IsLeaf = isLeaf;
Organizers = organizers ?? Enumerable.Empty<Organizer>();
}

Expand All @@ -35,5 +35,8 @@ public void AddOrganizer(Organizer organizer)
}
_organizers.Add(organizer);
}

public void MakeParent()
=> IsLeaf = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ namespace MiniSpace.Services.Organizations.Infrastructure.Mongo.Documents
public static class Extensions
{
public static Organization AsEntity(this OrganizationDocument document)
=> new Organization(document.Id, document.Name, document.ParentId, document.Organizers);
=> new Organization(document.Id, document.Name, document.ParentId, document.IsLeaf, document.Organizers);

public static OrganizationDocument AsDocument(this Organization entity)
=> new OrganizationDocument()
{
Id = entity.Id,
Name = entity.Name,
ParentId = entity.ParentId,
IsLeaf = entity.IsLeaf,
Organizers = entity.Organizers
};

Expand All @@ -23,7 +24,7 @@ public static OrganizationDto AsDto(this OrganizationDocument document)
Id = document.Id,
Name = document.Name,
ParentId = document.ParentId,
MemberCount = document.Organizers.Count()
IsLeaf = document.IsLeaf
};

public static Organizer AsEntity(this OrganizerDocument document)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class OrganizationDocument: IIdentifiable<Guid>
public Guid Id { get; set; }
public string Name { get; set; }
public Guid ParentId { get; set; }
public bool IsLeaf { get; set; }
public IEnumerable<Organizer> Organizers { get; set; }
}
}

0 comments on commit 08a711a

Please sign in to comment.