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

Authors crud #191

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions src/BookStore.Infrastructure/Migrations/20240429053129_first.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace BookStore.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class first : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{

}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{

}
}
}
12 changes: 9 additions & 3 deletions src/BookStore.Presentation/MVC/Controllers/AuthorsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,16 @@ public async Task<IActionResult> UpdateAsync(Author author)

var updatedAuthor = await _mediator.Send(updateAuthorCommand);

return View("Details", updatedAuthor);
var detailsModel = new AuthorDetailsViewModel
{
Author = updatedAuthor,
Host = HttpContext.Request.Host.ToString(),
};

return View("Details", detailsModel);
}

public async Task<IActionResult> DeleteAuthor(int id)
public async Task<IActionResult> DeleteAsync(int id)
{
var query = new DeleteAuthorCommand()
{
Expand All @@ -103,4 +109,4 @@ public async Task<IActionResult> DeleteAuthor(int id)
return RedirectToAction(actionName: nameof(Index));
}
}
}
}
38 changes: 38 additions & 0 deletions src/BookStore.Presentation/MVC/Controllers/CartController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,44 @@ public async Task<IActionResult> ClearAsync()
return RedirectToAction(nameof(Basket));
}

public async Task<IActionResult> DeleteAsync(int bookId)
{
HttpContext.Request.Cookies.TryGetValue("cart", out string cartItemsJson);
cartItemsJson ??= "[]";

var cartItems = JsonSerializer.Deserialize<List<CartItem>>(cartItemsJson);

if (cartItems == null)
{
return RedirectToAction(nameof(Basket));
}

var item = cartItems.FirstOrDefault(x => x.BookId == bookId);

if (item != null)
{
item.Count--;

if (item.Count <= 0)
{
cartItems.Remove(item);
}

HttpContext.Response.Cookies.Append(
"cart",
JsonSerializer.Serialize(
cartItems,
new JsonSerializerOptions()
{
ReferenceHandler = ReferenceHandler.IgnoreCycles
}
)
);
}

return RedirectToAction(nameof(Basket));
}

public async Task<IActionResult> CreateOrderAsync()
{
if (!HttpContext.User.Identity.IsAuthenticated)
Expand Down
3 changes: 3 additions & 0 deletions src/BookStore.Presentation/MVC/MVC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
<ItemGroup>
<Folder Include="wwwroot\AuthorsProfileImage\" />
<Folder Include="wwwroot\BookPhoto\" />
<Folder Include="wwwroot\BookPhoto\" />
<Folder Include="wwwroot\Books\" />
<Folder Include="wwwroot\AuthorsProfileImage\" />
<Folder Include="wwwroot\UserProfileImage\" />
</ItemGroup>

Expand Down
5 changes: 3 additions & 2 deletions src/BookStore.Presentation/MVC/Views/Authors/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@
@if (User.Identity.IsAuthenticated && User.IsInRole("SuperAdmin"))
{
<div class="card-body">
<a href="#" class="btn btn-warning">Update</a>
<a href="#" class="btn btn-danger">Delete</a>
<a href ="@Url.Action("Update", new { id = author.Id })" class="btn btn-warning">
Update</a>
<a a asp-controller="Authors" asp-action="Delete" asp-route-id="@author.Id" class="btn btn-danger">Delete</a>
</div>
}
</div>
Expand Down
46 changes: 43 additions & 3 deletions src/BookStore.Presentation/MVC/Views/Books/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,52 @@ else
}
else
{
<a asp-controller="Cart" asp-action="AddToCart" asp-route-bookId ="@book.Id" class="btn add-card-btn btn-outline-primary"><i class='bx bx-cart-add'></i></a>
<span class="price">@book.Price</span>
<span class=""> so'm</span>
<button onclick="addToCart('@book.Id', '@book.Title')" class="btn add-card-btn btn-outline-primary"><i class='bx bx-cart-add'></i></button>
<span class="price">@book.Price so'm</span>
}
</div>
</div>


<script>
function addToCart(bookId, bookTitle) {
$.ajax({
type: "POST",
url: "/Cart/AddToCart",
data: { bookId: bookId },
success: function () {
// Retrieve books count from cookie
var booksCount = getBooksCountFromCookie('cart');
// Show alert with book name and count of books added to basket
alert("Added " + bookTitle + " to Basket. Books in Basket: " + booksCount);
},
error: function () {
alert("Failed to add book to basket.");
}
});
}

function getBooksCountFromCookie(cookieName) {
var name = cookieName + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var cookieArray = decodedCookie.split(';');
for (var i = 0; i < cookieArray.length; i++) {
var cookie = cookieArray[i];
while (cookie.charAt(0) == ' ') {
cookie = cookie.substring(1);
}
if (cookie.indexOf(name) == 0) {
var value = cookie.substring(name.length, cookie.length);
var cart = JSON.parse(value);
return cart[0].Count; // Assuming count is the first element in the array
}
}
return 0; // Return 0 if cookie not found or count not available
}

</script>


}
</div>

Expand Down
9 changes: 8 additions & 1 deletion src/BookStore.Presentation/MVC/Views/Cart/Basket.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@
<th scope="col">Title</th>
<th scope="col">Price</th>
<th scope="col">Count</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Items.Count; i++)
{
<tr>
<th>
<img class="cart-photo" src="@Model.Items[i].Book.PhotoPath" alt="Photo path"/>
<img class="cart-photo" src="@Model.Items[i].Book.PhotoPath" alt="Photo path" />
</th>
<th>@Model.Items[i].Book.Title</th>
<td>@Model.Items[i].Book.Price</td>
<td>@Model.Items[i].Count</td>
<td>
<a asp-controller="Cart" asp-action="Delete" asp-route-bookId="@Model.Items[i].Book.Id" class="btn btn-danger">
<i class='bx bxs-trash'></i>
</a>
</td>

</tr>
}
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion src/BookStore.Presentation/MVC/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"BotToken": "7040566448:AAGuJLoJsg8xqAAW4yWGPkZDR6PIxBlU0ns",
"ConnectionStrings": {
"SQLServer": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=BookStore.MVC.DB;",
"Postgres": "Host=localhost;Port=5432;Username=postgres;Password=sardor0618!;Database=BookStoree.DB"
"Postgres": "Host=localhost;Port=5432;Username=postgres;Password=coder;Database=BookStoree.DB"
},
"Serilog": {
"Using": [ "Serilog.Sinks.File" ],
Expand Down