Skip to content

Commit

Permalink
update domain
Browse files Browse the repository at this point in the history
  • Loading branch information
iammukeshm committed Feb 21, 2025
1 parent c5567fc commit ad78a56
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 70 deletions.
53 changes: 28 additions & 25 deletions src/api/modules/Catalog/Catalog.Domain/Brand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,47 @@
namespace FSH.Starter.WebApi.Catalog.Domain;
public class Brand : AuditableEntity, IAggregateRoot
{
public string Name { get; private set; } = default!;
public string Name { get; private set; } = string.Empty;
public string? Description { get; private set; }

public static Brand Create(string name, string? description)
{
var brand = new Brand
{
Name = name,
Description = description
};
private Brand() { }

brand.QueueDomainEvent(new BrandCreated() { Brand = brand });
private Brand(Guid id, string name, string? description)
{
Id = id;
Name = name;
Description = description;
QueueDomainEvent(new BrandCreated { Brand = this });
}

return brand;
public static Brand Create(string name, string? description)
{
return new Brand(Guid.NewGuid(), name, description);
}

public Brand Update(string? name, string? description)
{
if (name is not null && Name?.Equals(name, StringComparison.OrdinalIgnoreCase) is not true) Name = name;
if (description is not null && Description?.Equals(description, StringComparison.OrdinalIgnoreCase) is not true) Description = description;

this.QueueDomainEvent(new BrandUpdated() { Brand = this });
bool isUpdated = false;

return this;
}
if (!string.IsNullOrWhiteSpace(name) && !string.Equals(Name, name, StringComparison.OrdinalIgnoreCase))
{
Name = name;
isUpdated = true;
}

public static Brand Update(Guid id, string name, string? description)
{
var brand = new Brand
if (!string.Equals(Description, description, StringComparison.OrdinalIgnoreCase))
{
Id = id,
Name = name,
Description = description
};
Description = description;
isUpdated = true;
}

brand.QueueDomainEvent(new BrandUpdated() { Brand = brand });
if (isUpdated)
{
QueueDomainEvent(new BrandUpdated { Brand = this });
}

return brand;
return this;
}
}


70 changes: 42 additions & 28 deletions src/api/modules/Catalog/Catalog.Domain/Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,64 @@
namespace FSH.Starter.WebApi.Catalog.Domain;
public class Product : AuditableEntity, IAggregateRoot
{
public string Name { get; private set; } = default!;
public string Name { get; private set; } = string.Empty;
public string? Description { get; private set; }
public decimal Price { get; private set; }
public Guid? BrandId { get; private set; }
public virtual Brand Brand { get; private set; } = default!;

public static Product Create(string name, string? description, decimal price, Guid? brandId)
{
var product = new Product();
private Product() { }

product.Name = name;
product.Description = description;
product.Price = price;
product.BrandId = brandId;
private Product(Guid id, string name, string? description, decimal price, Guid? brandId)
{
Id = id;
Name = name;
Description = description;
Price = price;
BrandId = brandId;

product.QueueDomainEvent(new ProductCreated() { Product = product });
QueueDomainEvent(new ProductCreated { Product = this });
}

return product;
public static Product Create(string name, string? description, decimal price, Guid? brandId)
{
return new Product(Guid.NewGuid(), name, description, price, brandId);
}

public Product Update(string? name, string? description, decimal? price, Guid? brandId)
{
if (name is not null && Name?.Equals(name, StringComparison.OrdinalIgnoreCase) is not true) Name = name;
if (description is not null && Description?.Equals(description, StringComparison.OrdinalIgnoreCase) is not true) Description = description;
if (price.HasValue && Price != price) Price = price.Value;
if (brandId.HasValue && brandId.Value != Guid.Empty && !BrandId.Equals(brandId.Value)) BrandId = brandId.Value;
bool isUpdated = false;

this.QueueDomainEvent(new ProductUpdated() { Product = this });
return this;
}
if (!string.IsNullOrWhiteSpace(name) && !string.Equals(Name, name, StringComparison.OrdinalIgnoreCase))
{
Name = name;
isUpdated = true;
}

public static Product Update(Guid id, string name, string? description, decimal price, Guid? brandId)
{
var product = new Product
if (!string.Equals(Description, description, StringComparison.OrdinalIgnoreCase))
{
Description = description;
isUpdated = true;
}

if (price.HasValue && Price != price.Value)
{
Price = price.Value;
isUpdated = true;
}

if (brandId.HasValue && brandId.Value != Guid.Empty && BrandId != brandId.Value)
{
Id = id,
Name = name,
Description = description,
Price = price,
BrandId = brandId
};
BrandId = brandId.Value;
isUpdated = true;
}

product.QueueDomainEvent(new ProductUpdated() { Product = product });
if (isUpdated)
{
QueueDomainEvent(new ProductUpdated { Product = this });
}

return product;
return this;
}
}

44 changes: 27 additions & 17 deletions src/api/modules/Todo/Domain/TodoItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,44 @@
using FSH.Starter.WebApi.Todo.Domain.Events;

namespace FSH.Starter.WebApi.Todo.Domain;
public class TodoItem : AuditableEntity, IAggregateRoot
public sealed class TodoItem : AuditableEntity, IAggregateRoot
{
public string? Title { get; set; }
public string Title { get; private set; } = string.Empty;
public string Note { get; private set; } = string.Empty;

public string? Note { get; set; }
private TodoItem() { }

public static TodoItem Create(string title, string note)
private TodoItem(string title, string note)
{
var item = new TodoItem();

item.Title = title;
item.Note = note;

item.QueueDomainEvent(new TodoItemCreated(item.Id, item.Title, item.Note));

Title = title;
Note = note;
QueueDomainEvent(new TodoItemCreated(Id, Title, Note));
TodoMetrics.Created.Add(1);

return item;
}

public static TodoItem Create(string title, string note) => new(title, note);

public TodoItem Update(string? title, string? note)
{
if (title is not null && Title?.Equals(title, StringComparison.OrdinalIgnoreCase) is not true) Title = title;
if (note is not null && Note?.Equals(note, StringComparison.OrdinalIgnoreCase) is not true) Note = note;
bool isUpdated = false;

this.QueueDomainEvent(new TodoItemUpdated(this));
if (!string.IsNullOrWhiteSpace(title) && !string.Equals(Title, title, StringComparison.OrdinalIgnoreCase))
{
Title = title;
isUpdated = true;
}

return this;
if (!string.IsNullOrWhiteSpace(note) && !string.Equals(Note, note, StringComparison.OrdinalIgnoreCase))
{
Note = note;
isUpdated = true;
}

if (isUpdated)
{
QueueDomainEvent(new TodoItemUpdated(this));
}

return this;
}
}

0 comments on commit ad78a56

Please sign in to comment.