Skip to content

Commit 19aad82

Browse files
committed
sepet tablosu eklendi, sepetim sayfasi olusturuldu, kitap detay sayfasinda satin al denince kitaplar kisiye ozel sepete eklenecek sekilde ayarlandi, sepetten kitap cikarma islevi yerine getirildi
1 parent b72e5b2 commit 19aad82

12 files changed

Lines changed: 1152 additions & 11 deletions
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Security.Claims;
5+
using System.Threading.Tasks;
6+
using BookstoreProject.Data;
7+
using BookstoreProject.Models;
8+
using Microsoft.AspNetCore.Authorization;
9+
using Microsoft.AspNetCore.Identity;
10+
using Microsoft.AspNetCore.Mvc;
11+
using Microsoft.EntityFrameworkCore;
12+
13+
namespace BookstoreProject.Controllers
14+
{
15+
public class BasketsController : Controller
16+
{
17+
private readonly ApplicationDbContext _context;
18+
private readonly UserManager<UserDetails> _userManager;
19+
20+
public BasketsController(ApplicationDbContext context, UserManager<UserDetails> userManager)
21+
{
22+
_context = context;
23+
_userManager = userManager;
24+
}
25+
26+
27+
[Authorize]
28+
public async Task<IActionResult> Index()
29+
{
30+
List<Basket> _basket = await _context.Baskets.Where(x => x.UserId.ToString() == User.FindFirstValue(ClaimTypes.NameIdentifier) && x.Active == true).Include(b => b.Book).ToListAsync();
31+
32+
ViewData["ToplamFiyat"] = _basket.Sum(x=> x.Book.Price);
33+
34+
return View(_basket);
35+
}
36+
37+
[Authorize]
38+
public async Task<IActionResult> removeFromBasket(int? bookId)
39+
{
40+
if (bookId == null)
41+
{
42+
return NotFound();
43+
}
44+
45+
Basket basket = await _context.Baskets.Where(x => x.UserId.ToString() == User.FindFirstValue(ClaimTypes.NameIdentifier) && x.Active == true && x.Book.Id == bookId).Include(b => b.Book).FirstOrDefaultAsync();
46+
47+
if (basket != null)
48+
{
49+
basket.Active = false;
50+
_context.Update(basket);
51+
await _context.SaveChangesAsync();
52+
}
53+
else
54+
{
55+
return NotFound();
56+
}
57+
58+
return RedirectToAction(nameof(Index));
59+
}
60+
}
61+
}

BookstoreProject/Controllers/HomeController.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.AspNetCore.Authorization;
1010
using BookstoreProject.Data;
1111
using Microsoft.EntityFrameworkCore;
12+
using System.Security.Claims;
1213

1314
namespace BookstoreProject.Controllers
1415
{
@@ -69,5 +70,22 @@ public IActionResult ErrorPage(int statusCode)
6970
}
7071
return View();
7172
}
73+
74+
[HttpPost]
75+
[Authorize]
76+
public async Task<IActionResult> AddBookToBasket(int bookId)
77+
{
78+
Basket _basket = new Basket
79+
{
80+
BookId = bookId,
81+
Active = true,
82+
UserId = Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier))
83+
};
84+
85+
_context.Add(_basket);
86+
await _context.SaveChangesAsync();
87+
88+
return RedirectToAction("Index", "Baskets");
89+
}
7290
}
7391
}

BookstoreProject/Data/ApplicationDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,7 @@ public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
2121
public DbSet<Language> Languages { get; set; }
2222

2323
public DbSet<Author> Authors { get; set; }
24+
25+
public DbSet<Basket> Baskets { get; set; }
2426
}
2527
}

0 commit comments

Comments
 (0)