Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7cca7d6

Browse files
Get and save dropdown (language) data from/to database
1 parent 2c1976b commit 7cca7d6

File tree

13 files changed

+287
-99
lines changed

13 files changed

+287
-99
lines changed

‎Webgentle.BookStore/Webgentle.BookStore/Controllers/BookController.cs‎

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ namespace Webgentle.BookStore.Controllers
1212
public class BookController : Controller
1313
{
1414
private readonly BookRepository _bookRepository = null;
15+
private readonly LanguageRepository _languageRepository = null;
1516

16-
public BookController(BookRepository bookRepository)
17+
public BookController(BookRepository bookRepository,LanguageRepositorylanguageRepository)
1718
{
1819
_bookRepository = bookRepository;
20+
_languageRepository = languageRepository;
1921
}
2022

2123
public async Task<ViewResult> GetAllBooks()
@@ -38,22 +40,11 @@ public List<BookModel> SearchBooks(string bookName, string authorName)
3840
return _bookRepository.SearchBook(bookName, authorName);
3941
}
4042

41-
public ViewResult AddNewBook(bool isSuccess = false, int bookId = 0)
43+
public asyncTask<ViewResult> AddNewBook(bool isSuccess = false, int bookId = 0)
4244
{
43-
var model = new BookModel()
44-
{
45-
//Language = "2"
46-
};
45+
var model = new BookModel();
4746

48-
//ViewBag.Language = new List<SelectListItem>()
49-
//{
50-
// new SelectListItem(){Text = "Hindi", Value = "1" },
51-
// new SelectListItem(){Text = "English", Value = "2" },
52-
// new SelectListItem(){Text = "Dutch", Value = "3"},
53-
// new SelectListItem(){Text = "Tamil", Value = "4"},
54-
// new SelectListItem(){Text = "Urdu", Value = "5" },
55-
// new SelectListItem(){Text = "Chinese", Value = "6"},
56-
//};
47+
ViewBag.Language = new SelectList(await _languageRepository.GetLanguages(), "Id", "Name");
5748

5849
ViewBag.IsSuccess = isSuccess;
5950
ViewBag.BookId = bookId;
@@ -72,28 +63,10 @@ public async Task<IActionResult> AddNewBook(BookModel bookModel)
7263
}
7364
}
7465

75-
76-
//ViewBag.Language = new List<SelectListItem>()
77-
//{
78-
// new SelectListItem(){Text = "Hindi", Value = "1" },
79-
// new SelectListItem(){Text = "English", Value = "2" },
80-
// new SelectListItem(){Text = "Dutch", Value = "3"},
81-
// new SelectListItem(){Text = "Tamil", Value = "4"},
82-
// new SelectListItem(){Text = "Urdu", Value = "5" },
83-
// new SelectListItem(){Text = "Chinese", Value = "6"},
84-
//};
66+
ViewBag.Language = new SelectList(await _languageRepository.GetLanguages(), "Id", "Name");
8567

86-
return View();
87-
}
8868

89-
private List<LanguageModel> GetLanguage()
90-
{
91-
return new List<LanguageModel>()
92-
{
93-
new LanguageModel(){ Id = 1, Text = "Hindi"},
94-
new LanguageModel(){ Id = 2, Text = "English"},
95-
new LanguageModel(){ Id = 3, Text = "Dutch"},
96-
};
69+
return View();
9770
}
9871
}
9972
}

‎Webgentle.BookStore/Webgentle.BookStore/Data/BookStoreContext.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ public BookStoreContext(DbContextOptions<BookStoreContext> options)
1515
}
1616

1717
public DbSet<Books> Books { get; set; }
18+
19+
public DbSet<Language> Language { get; set; }
1820
}
1921
}

‎Webgentle.BookStore/Webgentle.BookStore/Data/Books.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ public class Books
1212
public string Author { get; set; }
1313
public string Description { get; set; }
1414
public string Category { get; set; }
15-
public stringLanguage { get; set; }
15+
public intLanguageId { get; set; }
1616
public int TotalPages { get; set; }
1717
public DateTime? CreatedOn { get; set; }
1818
public DateTime? UpdatedOn { get; set; }
19+
20+
public Language Language { get; set; }
1921
}
2022
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace Webgentle.BookStore.Data
7+
{
8+
public class Language
9+
{
10+
public int Id { get; set; }
11+
public string Name { get; set; }
12+
public string Description { get; set; }
13+
14+
public ICollection<Books> Books { get; set; }
15+
}
16+
}

‎Webgentle.BookStore/Webgentle.BookStore/Migrations/20200601033059_AddedLanguageTable.Designer.cs‎

Lines changed: 91 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
namespace Webgentle.BookStore.Migrations
4+
{
5+
public partial class AddedLanguageTable : Migration
6+
{
7+
protected override void Up(MigrationBuilder migrationBuilder)
8+
{
9+
migrationBuilder.DropColumn(
10+
name: "Language",
11+
table: "Books");
12+
13+
migrationBuilder.AddColumn<int>(
14+
name: "LanguageId",
15+
table: "Books",
16+
nullable: false,
17+
defaultValue: 0);
18+
19+
migrationBuilder.CreateTable(
20+
name: "Language",
21+
columns: table => new
22+
{
23+
Id = table.Column<int>(nullable: false)
24+
.Annotation("SqlServer:Identity", "1, 1"),
25+
Name = table.Column<string>(nullable: true),
26+
Description = table.Column<string>(nullable: true)
27+
},
28+
constraints: table =>
29+
{
30+
table.PrimaryKey("PK_Language", x => x.Id);
31+
});
32+
33+
migrationBuilder.CreateIndex(
34+
name: "IX_Books_LanguageId",
35+
table: "Books",
36+
column: "LanguageId");
37+
38+
migrationBuilder.AddForeignKey(
39+
name: "FK_Books_Language_LanguageId",
40+
table: "Books",
41+
column: "LanguageId",
42+
principalTable: "Language",
43+
principalColumn: "Id",
44+
onDelete: ReferentialAction.Cascade);
45+
}
46+
47+
protected override void Down(MigrationBuilder migrationBuilder)
48+
{
49+
migrationBuilder.DropForeignKey(
50+
name: "FK_Books_Language_LanguageId",
51+
table: "Books");
52+
53+
migrationBuilder.DropTable(
54+
name: "Language");
55+
56+
migrationBuilder.DropIndex(
57+
name: "IX_Books_LanguageId",
58+
table: "Books");
59+
60+
migrationBuilder.DropColumn(
61+
name: "LanguageId",
62+
table: "Books");
63+
64+
migrationBuilder.AddColumn<string>(
65+
name: "Language",
66+
table: "Books",
67+
type: "nvarchar(max)",
68+
nullable: true);
69+
}
70+
}
71+
}

‎Webgentle.BookStore/Webgentle.BookStore/Migrations/BookStoreContextModelSnapshot.cs‎

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
1515
{
1616
#pragma warning disable 612, 618
1717
modelBuilder
18-
.HasAnnotation("ProductVersion", "3.1.3")
18+
.HasAnnotation("ProductVersion", "3.1.4")
1919
.HasAnnotation("Relational:MaxIdentifierLength", 128)
2020
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
2121

@@ -38,8 +38,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)
3838
b.Property<string>("Description")
3939
.HasColumnType("nvarchar(max)");
4040

41-
b.Property<string>("Language")
42-
.HasColumnType("nvarchar(max)");
41+
b.Property<int>("LanguageId")
42+
.HasColumnType("int");
4343

4444
b.Property<string>("Title")
4545
.HasColumnType("nvarchar(max)");
@@ -52,8 +52,37 @@ protected override void BuildModel(ModelBuilder modelBuilder)
5252

5353
b.HasKey("Id");
5454

55+
b.HasIndex("LanguageId");
56+
5557
b.ToTable("Books");
5658
});
59+
60+
modelBuilder.Entity("Webgentle.BookStore.Data.Language", b =>
61+
{
62+
b.Property<int>("Id")
63+
.ValueGeneratedOnAdd()
64+
.HasColumnType("int")
65+
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
66+
67+
b.Property<string>("Description")
68+
.HasColumnType("nvarchar(max)");
69+
70+
b.Property<string>("Name")
71+
.HasColumnType("nvarchar(max)");
72+
73+
b.HasKey("Id");
74+
75+
b.ToTable("Language");
76+
});
77+
78+
modelBuilder.Entity("Webgentle.BookStore.Data.Books", b =>
79+
{
80+
b.HasOne("Webgentle.BookStore.Data.Language", "Language")
81+
.WithMany("Books")
82+
.HasForeignKey("LanguageId")
83+
.OnDelete(DeleteBehavior.Cascade)
84+
.IsRequired();
85+
});
5786
#pragma warning restore 612, 618
5887
}
5988
}

‎Webgentle.BookStore/Webgentle.BookStore/Models/BookModel.cs‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@ public class BookModel
1919
public string Description { get; set; }
2020
public string Category { get; set; }
2121
//[Required(ErrorMessage = "Please choose the language of your book")]
22+
public int LanguageId { get; set; }
2223
public string Language { get; set; }
2324

24-
[Required(ErrorMessage = "Please choose the language of your book")]
25-
public LanguageEnum LanguageEnum { get; set; }
26-
27-
2825
[Required(ErrorMessage = "Please enter the total pages")]
2926
[Display(Name ="Total pages of book")]
3027
public int? TotalPages { get; set; }

‎Webgentle.BookStore/Webgentle.BookStore/Models/LanguageModel.cs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ namespace Webgentle.BookStore.Models
88
public class LanguageModel
99
{
1010
public int Id { get; set; }
11-
public string Text { get; set; }
11+
public string Name { get; set; }
12+
public string Description { get; set; }
1213
}
1314
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /