Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
lesson60
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Нурбек Созоев
lesson60
Commits
f0a43295
Commit
f0a43295
authored
Jun 06, 2019
by
Нурбек Созоев
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Добавил модель Supplier
parent
37433489
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
195 additions
and
5 deletions
+195
-5
ProductController.cs
Store/Controllers/ProductController.cs
+10
-3
20190605141259_AddSuppliersTable.Designer.cs
...e/Migrations/20190605141259_AddSuppliersTable.Designer.cs
+68
-0
20190605141259_AddSuppliersTable.cs
Store/Migrations/20190605141259_AddSuppliersTable.cs
+58
-0
ProductContextModelSnapshot.cs
Store/Migrations/ProductContextModelSnapshot.cs
+24
-0
Product.cs
Store/Models/Product.cs
+5
-0
ProductContext.cs
Store/Models/ProductContext.cs
+1
-0
Supplier.cs
Store/Models/Supplier.cs
+12
-0
Startup.cs
Store/Startup.cs
+2
-1
Store.csproj
Store/Store.csproj
+0
-1
Create.cshtml
Store/Views/Product/Create.cshtml
+11
-0
Details.cshtml
Store/Views/Product/Details.cshtml
+4
-0
No files found.
Store/Controllers/ProductController.cs
View file @
f0a43295
using
System.Collections.Generic
;
using
System.Linq
;
using
Microsoft.AspNetCore.Mvc
;
using
Microsoft.EntityFrameworkCore
;
using
Store.Models
;
namespace
Store.Controllers
...
...
@@ -20,16 +22,21 @@ namespace Store.Controllers
public
IActionResult
Details
(
string
id
)
{
Product
product
=
_context
.
Products
.
FirstOrDefault
(
p
=>
p
.
Id
==
id
);
Product
product
=
_context
.
Products
.
Include
(
p
=>
p
.
Supplier
)
.
First
(
p
=>
p
.
Id
.
ToString
()
==
id
);
ViewBag
.
product
=
product
;
ViewBag
.
supplier
=
product
.
Supplier
;
return
View
();
}
[
ActionName
(
"CreateProduct"
)]
[
HttpGet
]
public
IActionResult
Create
()
{
return
View
();
List
<
Supplier
>
suppliers
=
_context
.
Suppliers
.
ToList
();
ViewBag
.
suppliers
=
suppliers
;
return
View
(
new
Product
());
}
[
HttpPost
]
...
...
Store/Migrations/20190605141259_AddSuppliersTable.Designer.cs
0 → 100644
View file @
f0a43295
// <auto-generated />
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.EntityFrameworkCore.Infrastructure
;
using
Microsoft.EntityFrameworkCore.Migrations
;
using
Microsoft.EntityFrameworkCore.Storage.ValueConversion
;
using
Npgsql.EntityFrameworkCore.PostgreSQL.Metadata
;
using
Store.Models
;
namespace
Store.Migrations
{
[
DbContext
(
typeof
(
ProductContext
))]
[
Migration
(
"20190605141259_AddSuppliersTable"
)]
partial
class
AddSuppliersTable
{
protected
override
void
BuildTargetModel
(
ModelBuilder
modelBuilder
)
{
#pragma warning disable 612, 618
modelBuilder
.
HasAnnotation
(
"Npgsql:ValueGenerationStrategy"
,
NpgsqlValueGenerationStrategy
.
SerialColumn
)
.
HasAnnotation
(
"ProductVersion"
,
"2.2.4-servicing-10062"
)
.
HasAnnotation
(
"Relational:MaxIdentifierLength"
,
63
);
modelBuilder
.
Entity
(
"Store.Models.Product"
,
b
=>
{
b
.
Property
<
string
>(
"Id"
)
.
ValueGeneratedOnAdd
();
b
.
Property
<
int
>(
"Amount"
);
b
.
Property
<
string
>(
"Brand"
);
b
.
Property
<
string
>(
"Category"
);
b
.
Property
<
string
>(
"Description"
);
b
.
Property
<
string
>(
"Name"
);
b
.
Property
<
string
>(
"SupplierId"
);
b
.
HasKey
(
"Id"
);
b
.
HasIndex
(
"SupplierId"
);
b
.
ToTable
(
"Products"
);
});
modelBuilder
.
Entity
(
"Store.Models.Supplier"
,
b
=>
{
b
.
Property
<
string
>(
"Id"
)
.
ValueGeneratedOnAdd
();
b
.
Property
<
string
>(
"Name"
);
b
.
HasKey
(
"Id"
);
b
.
ToTable
(
"Supplier"
);
});
modelBuilder
.
Entity
(
"Store.Models.Product"
,
b
=>
{
b
.
HasOne
(
"Store.Models.Supplier"
,
"Supplier"
)
.
WithMany
(
"Products"
)
.
HasForeignKey
(
"SupplierId"
);
});
#pragma warning restore 612, 618
}
}
}
Store/Migrations/20190605141259_AddSuppliersTable.cs
0 → 100644
View file @
f0a43295
using
Microsoft.EntityFrameworkCore.Migrations
;
namespace
Store.Migrations
{
public
partial
class
AddSuppliersTable
:
Migration
{
protected
override
void
Up
(
MigrationBuilder
migrationBuilder
)
{
migrationBuilder
.
AddColumn
<
string
>(
name
:
"SupplierId"
,
table
:
"Products"
,
nullable
:
true
);
migrationBuilder
.
CreateTable
(
name
:
"Suppliers"
,
columns
:
table
=>
new
{
Id
=
table
.
Column
<
string
>(
nullable
:
false
),
Name
=
table
.
Column
<
string
>(
nullable
:
true
)
},
constraints
:
table
=>
{
table
.
PrimaryKey
(
"PK_Supplier"
,
x
=>
x
.
Id
);
});
migrationBuilder
.
CreateIndex
(
name
:
"IX_Products_SupplierId"
,
table
:
"Products"
,
column
:
"SupplierId"
);
migrationBuilder
.
AddForeignKey
(
name
:
"FK_Products_Supplier_SupplierId"
,
table
:
"Products"
,
column
:
"SupplierId"
,
principalTable
:
"Suppliers"
,
principalColumn
:
"Id"
,
onDelete
:
ReferentialAction
.
Restrict
);
}
protected
override
void
Down
(
MigrationBuilder
migrationBuilder
)
{
migrationBuilder
.
DropForeignKey
(
name
:
"FK_Products_Supplier_SupplierId"
,
table
:
"Products"
);
migrationBuilder
.
DropTable
(
name
:
"Suppliers"
);
migrationBuilder
.
DropIndex
(
name
:
"IX_Products_SupplierId"
,
table
:
"Products"
);
migrationBuilder
.
DropColumn
(
name
:
"SupplierId"
,
table
:
"Products"
);
}
}
}
Store/Migrations/ProductContextModelSnapshot.cs
View file @
f0a43295
// <auto-generated />
using
System
;
using
Microsoft.EntityFrameworkCore
;
using
Microsoft.EntityFrameworkCore.Infrastructure
;
using
Microsoft.EntityFrameworkCore.Storage.ValueConversion
;
...
...
@@ -33,10 +34,33 @@ namespace Store.Migrations
b
.
Property
<
string
>(
"Name"
);
b
.
Property
<
string
>(
"SupplierId"
);
b
.
HasKey
(
"Id"
);
b
.
HasIndex
(
"SupplierId"
);
b
.
ToTable
(
"Products"
);
});
modelBuilder
.
Entity
(
"Store.Models.Supplier"
,
b
=>
{
b
.
Property
<
string
>(
"Id"
)
.
ValueGeneratedOnAdd
();
b
.
Property
<
string
>(
"Name"
);
b
.
HasKey
(
"Id"
);
b
.
ToTable
(
"Suppliers"
);
});
modelBuilder
.
Entity
(
"Store.Models.Product"
,
b
=>
{
b
.
HasOne
(
"Store.Models.Supplier"
,
"Supplier"
)
.
WithMany
(
"Products"
)
.
HasForeignKey
(
"SupplierId"
);
});
#pragma warning restore 612, 618
}
}
...
...
Store/Models/Product.cs
View file @
f0a43295
using
System
;
namespace
Store.Models
{
public
class
Product
...
...
@@ -8,5 +10,8 @@ namespace Store.Models
public
string
Brand
{
get
;
set
;
}
public
int
Amount
{
get
;
set
;
}
public
string
Description
{
get
;
set
;
}
public
string
SupplierId
{
get
;
set
;
}
public
virtual
Supplier
Supplier
{
get
;
set
;
}
}
}
Store/Models/ProductContext.cs
View file @
f0a43295
...
...
@@ -5,6 +5,7 @@ namespace Store.Models
public
class
ProductContext
:
DbContext
{
public
DbSet
<
Product
>
Products
{
get
;
set
;
}
public
DbSet
<
Supplier
>
Suppliers
{
get
;
set
;
}
public
ProductContext
(
DbContextOptions
<
ProductContext
>
options
)
:
base
(
options
)
...
...
Store/Models/Supplier.cs
0 → 100644
View file @
f0a43295
using
System.Collections.Generic
;
namespace
Store.Models
{
public
class
Supplier
{
public
string
Id
{
get
;
set
;
}
public
string
Name
{
get
;
set
;
}
public
virtual
ICollection
<
Product
>
Products
{
get
;
set
;
}
}
}
Store/Startup.cs
View file @
f0a43295
...
...
@@ -29,7 +29,8 @@ namespace Store
});
string
connection
=
Configuration
.
GetConnectionString
(
"StoreConnection"
);
services
.
AddDbContext
<
ProductContext
>(
options
=>
options
.
UseNpgsql
(
connection
));
services
.
AddDbContext
<
ProductContext
>(
options
=>
options
.
UseNpgsql
(
connection
));
services
.
AddMvc
().
SetCompatibilityVersion
(
CompatibilityVersion
.
Version_2_1
);
}
...
...
Store/Store.csproj
View file @
f0a43295
...
...
@@ -6,7 +6,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.1.3" />
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.2" />
<PackageReference Include="Microsoft.AspNetCore.CookiePolicy" Version="2.1.1" />
<PackageReference Include="Microsoft.AspNetCore.HttpsPolicy" Version="2.1.1" />
...
...
Store/Views/Product/Create
Product
.cshtml
→
Store/Views/Product/Create.cshtml
View file @
f0a43295
@using Store.Models
@model Store.Models.Product
@{
Layout = "_MainLayout";
...
...
@@ -32,6 +33,16 @@
<input type="text" class="form-control" value="@Model.Description" id="Description" name="Description" placeholder="Enter amount">
</div>
<div class="form-group">
<label for="">Supplier</label>
<select name="SupplierId">
@foreach (Supplier s in ViewBag.suppliers)
{
<option value="@s.Id">@s.Name</option>
}
</select>
</div>
<button type="submit" class="btn btn-primary">Create</button>
</form>
Store/Views/Product/Details.cshtml
View file @
f0a43295
...
...
@@ -21,5 +21,9 @@
<td>Amount:</td>
<td>@ViewBag.product.Amount</td>
</tr>
<tr>
<td>Supplier Name:</td>
<td>@ViewBag.supplier.Name</td>
</tr>
</tbody>
</table>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment