Skip to content

Commit 69fc19c

Browse files
committed
Added Category entity.
1 parent 2d67950 commit 69fc19c

File tree

12 files changed

+201
-27
lines changed

12 files changed

+201
-27
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using Abp.AutoMapper;
3+
4+
namespace AbpCompanyName.AbpProjectName.Products.Dtos
5+
{
6+
[AutoMapTo(typeof(Product))]
7+
public class ProductCreateInput
8+
{
9+
public Guid CategoryId { get; set; }
10+
11+
public string Name { get; set; }
12+
13+
public float? Price { get; set; }
14+
}
15+
}

src/AbpCompanyName.AbpProjectName.Application/Products/Dtos/ProductDto.cs renamed to src/AbpCompanyName.AbpProjectName.Application/Products/Dtos/ProductListDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace AbpCompanyName.AbpProjectName.Products.Dtos
66
{
77
[AutoMapFrom(typeof(Product))]
8-
public class ProductDto : EntityDto
8+
public class ProductListDto : AuditedEntityDto
99
{
1010
public Guid CategoryId { get; set; }
1111

src/AbpCompanyName.AbpProjectName.Application/Products/IProductAppService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ namespace AbpCompanyName.AbpProjectName.Products
77
{
88
public interface IProductAppService : IApplicationService
99
{
10-
Task<ListResultOutput<ProductDto>> GetAllProducts();
10+
Task<ListResultOutput<ProductListDto>> GetAllProducts();
1111
}
1212
}
Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using System.Threading.Tasks;
35
using Abp.Application.Services.Dto;
46
using Abp.Domain.Repositories;
@@ -10,22 +12,39 @@ namespace AbpCompanyName.AbpProjectName.Products
1012
public class ProductAppService : AbpProjectNameAppServiceBase, IProductAppService
1113
{
1214
private readonly IRepository<Product> _productRepository;
15+
private readonly IRepository<Category, Guid> _categoryRepository;
1316

14-
public ProductAppService(IRepository<Product> productRepository)
17+
public ProductAppService(
18+
IRepository<Product> productRepository,
19+
IRepository<Category, Guid> categoryRepository)
1520
{
1621
_productRepository = productRepository;
22+
_categoryRepository = categoryRepository;
1723
}
1824

19-
public async Task<ListResultOutput<ProductDto>> GetAllProducts()
25+
public async Task<ListResultOutput<ProductListDto>> GetAllProducts()
2026
{
2127
var products = await _productRepository
2228
.GetAll()
2329
.Include(product => product.Category)
2430
.ToListAsync();
2531

26-
return new ListResultOutput<ProductDto>(
27-
ObjectMapper.Map<List<ProductDto>>(products)
32+
return new ListResultOutput<ProductListDto>(
33+
ObjectMapper.Map<List<ProductListDto>>(products)
2834
);
2935
}
36+
37+
public void Create(ProductCreateInput input)
38+
{
39+
var product = ObjectMapper.Map<Product>(input);
40+
41+
product.Category = input.CategoryId == default(Guid)
42+
? _categoryRepository.GetAll().First()
43+
: _categoryRepository.Get(input.CategoryId);
44+
45+
_productRepository.Insert(product);
46+
47+
CurrentUnitOfWork.SaveChanges();
48+
}
3049
}
3150
}

src/AbpCompanyName.AbpProjectName.Core/Products/Category.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace AbpCompanyName.AbpProjectName.Products
66
{
7+
[Table("Categories")]
78
public class Category : Entity<Guid>
89
{
910
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

src/AbpCompanyName.AbpProjectName.Core/Products/Product.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
22
using System.ComponentModel.DataAnnotations;
3+
using System.ComponentModel.DataAnnotations.Schema;
34
using Abp.Domain.Entities.Auditing;
45
using Abp.Extensions;
56

67
namespace AbpCompanyName.AbpProjectName.Products
78
{
9+
[Table("Products")]
810
public class Product : AuditedEntity
911
{
1012
public const int MaxNameLength = 128;

src/AbpCompanyName.AbpProjectName.EntityFrameworkCore/EntityFrameworkCore/AbpProjectNameDbContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace AbpCompanyName.AbpProjectName.EntityFrameworkCore
66
{
77
public class AbpProjectNameDbContext : AbpDbContext
88
{
9-
//TODO: Define an DbSet for each Entity...
9+
public DbSet<Category> Categories { get; set; }
1010

1111
public DbSet<Product> Products { get; set; }
1212

src/AbpCompanyName.AbpProjectName.EntityFrameworkCore/Migrations/20160722131107_Changed_Category_Table_Name.Designer.cs

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Microsoft.EntityFrameworkCore.Migrations;
4+
5+
namespace AbpCompanyName.AbpProjectName.EntityFrameworkCore.Migrations
6+
{
7+
public partial class Changed_Category_Table_Name : Migration
8+
{
9+
protected override void Up(MigrationBuilder migrationBuilder)
10+
{
11+
migrationBuilder.DropForeignKey(
12+
name: "FK_Products_Category_CategoryId",
13+
table: "Products");
14+
15+
migrationBuilder.DropPrimaryKey(
16+
name: "PK_Category",
17+
table: "Category");
18+
19+
migrationBuilder.AddPrimaryKey(
20+
name: "PK_Categories",
21+
table: "Category",
22+
column: "Id");
23+
24+
migrationBuilder.AddForeignKey(
25+
name: "FK_Products_Categories_CategoryId",
26+
table: "Products",
27+
column: "CategoryId",
28+
principalTable: "Category",
29+
principalColumn: "Id",
30+
onDelete: ReferentialAction.Cascade);
31+
32+
migrationBuilder.RenameTable(
33+
name: "Category",
34+
newName: "Categories");
35+
}
36+
37+
protected override void Down(MigrationBuilder migrationBuilder)
38+
{
39+
migrationBuilder.DropForeignKey(
40+
name: "FK_Products_Categories_CategoryId",
41+
table: "Products");
42+
43+
migrationBuilder.DropPrimaryKey(
44+
name: "PK_Categories",
45+
table: "Categories");
46+
47+
migrationBuilder.AddPrimaryKey(
48+
name: "PK_Category",
49+
table: "Categories",
50+
column: "Id");
51+
52+
migrationBuilder.AddForeignKey(
53+
name: "FK_Products_Category_CategoryId",
54+
table: "Products",
55+
column: "CategoryId",
56+
principalTable: "Categories",
57+
principalColumn: "Id",
58+
onDelete: ReferentialAction.Cascade);
59+
60+
migrationBuilder.RenameTable(
61+
name: "Categories",
62+
newName: "Category");
63+
}
64+
}
65+
}

src/AbpCompanyName.AbpProjectName.EntityFrameworkCore/Migrations/AbpProjectNameDbContextModelSnapshot.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
2525

2626
b.HasKey("Id");
2727

28-
b.ToTable("Category");
28+
b.ToTable("Categories");
2929
});
3030

3131
modelBuilder.Entity("AbpCompanyName.AbpProjectName.Products.Product", b =>

0 commit comments

Comments
 (0)