Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/catalog brand #1057

Merged
merged 14 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Shared/Authorization/FshPermissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public static class FshPermissions
new("Delete Products", FshActions.Delete, FshResources.Products),
new("Export Products", FshActions.Export, FshResources.Products),

//brands
new("View Brands", FshActions.View, FshResources.Brands, IsBasic: true),
new("Search Brands", FshActions.Search, FshResources.Brands, IsBasic: true),
new("Create Brands", FshActions.Create, FshResources.Brands),
new("Update Brands", FshActions.Update, FshResources.Brands),
new("Delete Brands", FshActions.Delete, FshResources.Brands),
new("Export Brands", FshActions.Export, FshResources.Brands),

//todos
new("View Todos", FshActions.View, FshResources.Todos, IsBasic: true),
new("Search Todos", FshActions.Search, FshResources.Todos, IsBasic: true),
Expand Down

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace FSH.Starter.WebApi.Migrations.PostgreSQL.Catalog
{
/// <inheritdoc />
public partial class AddCatalogSchema : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.EnsureSchema(
name: "catalog");

migrationBuilder.CreateTable(
name: "Brands",
schema: "catalog",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
Description = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true),
TenantId = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
Created = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
CreatedBy = table.Column<Guid>(type: "uuid", nullable: false),
LastModified = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
LastModifiedBy = table.Column<Guid>(type: "uuid", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Brands", x => x.Id);
});

migrationBuilder.CreateTable(
name: "Products",
schema: "catalog",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Name = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
Description = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true),
Price = table.Column<decimal>(type: "numeric", nullable: false),
BrandId = table.Column<Guid>(type: "uuid", nullable: true),
TenantId = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: false),
Created = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
CreatedBy = table.Column<Guid>(type: "uuid", nullable: false),
LastModified = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false),
LastModifiedBy = table.Column<Guid>(type: "uuid", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Products", x => x.Id);
table.ForeignKey(
name: "FK_Products_Brands_BrandId",
column: x => x.BrandId,
principalSchema: "catalog",
principalTable: "Brands",
principalColumn: "Id");
});

migrationBuilder.CreateIndex(
name: "IX_Products_BrandId",
schema: "catalog",
table: "Products",
column: "BrandId");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Products",
schema: "catalog");

migrationBuilder.DropTable(
name: "Brands",
schema: "catalog");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,59 @@ protected override void BuildModel(ModelBuilder modelBuilder)
#pragma warning disable 612, 618
modelBuilder
.HasDefaultSchema("catalog")
.HasAnnotation("ProductVersion", "8.0.6")
.HasAnnotation("ProductVersion", "8.0.8")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);

modelBuilder.Entity("FSH.Starter.WebApi.Catalog.Domain.Brand", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");

b.Property<DateTimeOffset>("Created")
.HasColumnType("timestamp with time zone");

b.Property<Guid>("CreatedBy")
.HasColumnType("uuid");

b.Property<string>("Description")
.HasMaxLength(1000)
.HasColumnType("character varying(1000)");

b.Property<DateTimeOffset>("LastModified")
.HasColumnType("timestamp with time zone");

b.Property<Guid?>("LastModifiedBy")
.HasColumnType("uuid");

b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("character varying(100)");

b.Property<string>("TenantId")
.IsRequired()
.HasMaxLength(64)
.HasColumnType("character varying(64)");

b.HasKey("Id");

b.ToTable("Brands", "catalog");

b.HasAnnotation("Finbuckle:MultiTenant", true);
});

modelBuilder.Entity("FSH.Starter.WebApi.Catalog.Domain.Product", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uuid");

b.Property<Guid?>("BrandId")
.HasColumnType("uuid");

b.Property<DateTimeOffset>("Created")
.HasColumnType("timestamp with time zone");

Expand Down Expand Up @@ -60,10 +102,21 @@ protected override void BuildModel(ModelBuilder modelBuilder)

b.HasKey("Id");

b.HasIndex("BrandId");

b.ToTable("Products", "catalog");

b.HasAnnotation("Finbuckle:MultiTenant", true);
});

modelBuilder.Entity("FSH.Starter.WebApi.Catalog.Domain.Product", b =>
{
b.HasOne("FSH.Starter.WebApi.Catalog.Domain.Brand", "Brand")
.WithMany()
.HasForeignKey("BrandId");

b.Navigation("Brand");
});
#pragma warning restore 612, 618
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.ComponentModel;
using MediatR;

namespace FSH.Starter.WebApi.Catalog.Application.Brands.Create.v1;
public sealed record CreateBrandCommand(
[property: DefaultValue("Sample Brand")] string? Name,
[property: DefaultValue("Descriptive Description")] string? Description = null) : IRequest<CreateBrandResponse>;

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using FluentValidation;

namespace FSH.Starter.WebApi.Catalog.Application.Brands.Create.v1;
public class CreateBrandCommandValidator : AbstractValidator<CreateBrandCommand>
{
public CreateBrandCommandValidator()
{
RuleFor(b => b.Name).NotEmpty().MinimumLength(2).MaximumLength(100);
RuleFor(b => b.Description).MaximumLength(1000);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using FSH.Framework.Core.Persistence;
using FSH.Starter.WebApi.Catalog.Domain;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace FSH.Starter.WebApi.Catalog.Application.Brands.Create.v1;
public sealed class CreateBrandHandler(
ILogger<CreateBrandHandler> logger,
[FromKeyedServices("catalog:brands")] IRepository<Brand> repository)
: IRequestHandler<CreateBrandCommand, CreateBrandResponse>
{
public async Task<CreateBrandResponse> Handle(CreateBrandCommand request, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(request);
var brand = Brand.Create(request.Name!, request.Description);
await repository.AddAsync(brand, cancellationToken);
logger.LogInformation("brand created {BrandId}", brand.Id);
return new CreateBrandResponse(brand.Id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace FSH.Starter.WebApi.Catalog.Application.Brands.Create.v1;

public sealed record CreateBrandResponse(Guid? Id);

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using MediatR;

namespace FSH.Starter.WebApi.Catalog.Application.Brands.Delete.v1;
public sealed record DeleteBrandCommand(
Guid Id) : IRequest;
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using FSH.Framework.Core.Persistence;
using FSH.Starter.WebApi.Catalog.Domain;
using FSH.Starter.WebApi.Catalog.Domain.Exceptions;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace FSH.Starter.WebApi.Catalog.Application.Brands.Delete.v1;
public sealed class DeleteBrandHandler(
ILogger<DeleteBrandHandler> logger,
[FromKeyedServices("catalog:brands")] IRepository<Brand> repository)
: IRequestHandler<DeleteBrandCommand>
{
public async Task Handle(DeleteBrandCommand request, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(request);
var brand = await repository.GetByIdAsync(request.Id, cancellationToken);
_ = brand ?? throw new BrandNotFoundException(request.Id);
await repository.DeleteAsync(brand, cancellationToken);
logger.LogInformation("Brand with id : {BrandId} deleted", brand.Id);
}
}
Loading
Loading