Skip to content

Commit

Permalink
All admin CRUD fixed
Browse files Browse the repository at this point in the history
Fixed:
- Remaining UserLib crud
  • Loading branch information
RespectMathias committed Dec 8, 2024
1 parent 2127e52 commit 9400d02
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 52 deletions.
Binary file modified GameLibrary/Database.db-shm
Binary file not shown.
Binary file modified GameLibrary/Database.db-wal
Binary file not shown.
15 changes: 14 additions & 1 deletion GameLibrary/Pages/Admin/Reviews/Edit.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
// Copyright 2024 Web.Tech. Group17
//
// Licensed under the Apache License, Version 2.0 (the "License"):
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using GameLibrary.Data;
using GameLibrary.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
Expand Down
1 change: 0 additions & 1 deletion GameLibrary/Pages/Admin/UserFavorites/Edit.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

using GameLibrary.Data;
using GameLibrary.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
Expand Down
35 changes: 21 additions & 14 deletions GameLibrary/Pages/Admin/UserLibraries/Create.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,41 @@
<div asp-validation-summary="ModelOnly" class="text-danger"></div>

<div class="form-group">
<label asp-for="UserLibrary!.UserId" class="control-label">User</label>
<select asp-for="UserLibrary!.UserId" class="form-control" asp-items="Model.Users">
<label asp-for="SelectedUserId" class="control-label">User</label>
<select asp-for="SelectedUserId" class="form-control" asp-items="Model.Users" required>
<option value="">Select a user</option>
</select>
<span asp-validation-for="UserLibrary!.UserId" class="text-danger"></span>
<span asp-validation-for="SelectedUserId" class="text-danger"></span>
</div>

<div class="form-group">
<label asp-for="UserLibrary!.GameId" class="control-label">Game</label>
<select asp-for="UserLibrary!.GameId" class="form-control" asp-items="Model.Games">
<label asp-for="SelectedGameId" class="control-label">Game</label>
<select asp-for="SelectedGameId" class="form-control" asp-items="Model.Games" required>
<option value="">Select a game</option>
</select>
<span asp-validation-for="UserLibrary!.GameId" class="text-danger"></span>
<span asp-validation-for="SelectedGameId" class="text-danger"></span>
</div>

<div class="form-group">
<label asp-for="UserLibrary!.Status" class="control-label">Status</label>
<input asp-for="UserLibrary!.Status" class="form-control" />
<span asp-validation-for="UserLibrary!.Status" class="text-danger"></span>
<label asp-for="SelectedStatus" class="control-label">Status</label>
<select asp-for="SelectedStatus" class="form-select" required>
<option value="">Select Status...</option>
<option value="Playing">Currently Playing</option>
<option value="Completed">Completed</option>
<option value="OnHold">On Hold</option>
<option value="Dropped">Dropped</option>
<option value="PlanToPlay">Plan to Play</option>
</select>
<span asp-validation-for="SelectedStatus" class="text-danger"></span>
</div>

<div class="form-group">
<label asp-for="UserLibrary!.IsUpcoming" class="control-label">Is Upcoming</label>
<input asp-for="UserLibrary!.IsUpcoming" class="form-control" />
<span asp-validation-for="UserLibrary!.IsUpcoming" class="text-danger"></span>
<div class="form-group form-check">
<input asp-for="SelectedIsUpcoming" class="form-check-input" type="checkbox" />
<label asp-for="SelectedIsUpcoming" class="form-check-label">Is Upcoming</label>
<span asp-validation-for="SelectedIsUpcoming" class="text-danger"></span>
</div>

<div class="form-group">
<div class="form-group mt-3">
<input type="submit" value="Create" class="btn btn-primary" style="background-color: #420599; color: #FFD43B;" />
<a asp-page="Index" class="btn" style="background-color: #420599; color: #FFD43B;">Back to List</a>
</div>
Expand Down
30 changes: 25 additions & 5 deletions GameLibrary/Pages/Admin/UserLibraries/Create.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,45 @@ public CreateModel(ApplicationDbContext context)
public SelectList Games { get; set; } = null!;
public SelectList Users { get; set; } = null!;

[BindProperty]
public Guid SelectedUserId { get; set; }

[BindProperty]
public Guid SelectedGameId { get; set; }

[BindProperty]
public string SelectedStatus { get; set; } = string.Empty;

[BindProperty]
public bool SelectedIsUpcoming { get; set; }

public IActionResult OnGet()
{
Games = new SelectList(_context.Games, "Id", "Title");
Users = new SelectList(_context.Users, "Id", "UserName");
return Page();
}

[BindProperty]
public UserLibrary? UserLibrary { get; set; }

public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
// Re-populate lists if there's a validation error
Games = new SelectList(_context.Games, "Id", "Title");
Users = new SelectList(_context.Users, "Id", "UserName");
return Page();
}

UserLibrary!.AddedDate = DateTime.UtcNow;
_context.UserLibraries.Add(UserLibrary);
var userLibrary = new UserLibrary
{
UserId = SelectedUserId,
GameId = SelectedGameId,
Status = SelectedStatus,
IsUpcoming = SelectedIsUpcoming,
AddedDate = DateTime.UtcNow
};

_context.UserLibraries.Add(userLibrary);
await _context.SaveChangesAsync();

return RedirectToPage("./Index");
Expand Down
39 changes: 24 additions & 15 deletions GameLibrary/Pages/Admin/UserLibraries/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,46 @@
<div class="col-md-4">
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="UserLibrary!.Id" />

<!-- Hidden field for the primary key (Id) -->
<input type="hidden" asp-for="SelectedId" />

<div class="form-group">
<label asp-for="UserLibrary!.UserId" class="control-label">User</label>
<select asp-for="UserLibrary!.UserId" class="form-control" asp-items="Model.Users">
<label asp-for="SelectedUserId" class="control-label">User</label>
<select asp-for="SelectedUserId" class="form-control" asp-items="Model.Users" required>
<option value="">Select a user</option>
</select>
<span asp-validation-for="UserLibrary!.UserId" class="text-danger"></span>
<span asp-validation-for="SelectedUserId" class="text-danger"></span>
</div>

<div class="form-group">
<label asp-for="UserLibrary!.GameId" class="control-label">Game</label>
<select asp-for="UserLibrary!.GameId" class="form-control" asp-items="Model.Games">
<label asp-for="SelectedGameId" class="control-label">Game</label>
<select asp-for="SelectedGameId" class="form-control" asp-items="Model.Games" required>
<option value="">Select a game</option>
</select>
<span asp-validation-for="UserLibrary!.GameId" class="text-danger"></span>
<span asp-validation-for="SelectedGameId" class="text-danger"></span>
</div>

<div class="form-group">
<label asp-for="UserLibrary!.Status" class="control-label">Status</label>
<input asp-for="UserLibrary!.Status" class="form-control" />
<span asp-validation-for="UserLibrary!.Status" class="text-danger"></span>
<label asp-for="SelectedStatus" class="control-label">Status</label>
<select asp-for="SelectedStatus" class="form-select" required>
<option value="">Select Status...</option>
<option value="Playing">Currently Playing</option>
<option value="Completed">Completed</option>
<option value="OnHold">On Hold</option>
<option value="Dropped">Dropped</option>
<option value="PlanToPlay">Plan to Play</option>
</select>
<span asp-validation-for="SelectedStatus" class="text-danger"></span>
</div>

<div class="form-group">
<label asp-for="UserLibrary!.IsUpcoming" class="control-label">Is Upcoming</label>
<input asp-for="UserLibrary!.IsUpcoming" class="form-control" />
<span asp-validation-for="UserLibrary!.IsUpcoming" class="text-danger"></span>
<div class="form-group form-check">
<input asp-for="SelectedIsUpcoming" class="form-check-input" type="checkbox" />
<label asp-for="SelectedIsUpcoming" class="form-check-label">Is Upcoming</label>
<span asp-validation-for="SelectedIsUpcoming" class="text-danger"></span>
</div>

<div class="form-group">
<div class="form-group mt-3">
<input type="submit" value="Save" class="btn btn-primary" style="background-color: #420599; color: #FFD43B;" />
<a asp-page="Index" class="btn" style="background-color: #420599; color: #FFD43B;">Back to List</a>
</div>
Expand Down
56 changes: 42 additions & 14 deletions GameLibrary/Pages/Admin/UserLibraries/Edit.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// limitations under the License.

using GameLibrary.Data;
using GameLibrary.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
Expand All @@ -30,31 +29,46 @@ public EditModel(ApplicationDbContext context)
_context = context;
}

[BindProperty]
public UserLibrary? UserLibrary { get; set; }

public SelectList? Games { get; set; }
public SelectList? Users { get; set; }

[BindProperty]
public Guid SelectedId { get; set; }

[BindProperty]
public Guid SelectedUserId { get; set; }

[BindProperty]
public Guid SelectedGameId { get; set; }

[BindProperty]
public string SelectedStatus { get; set; } = string.Empty;

[BindProperty]
public bool SelectedIsUpcoming { get; set; }

public async Task<IActionResult> OnGetAsync(Guid? id)
{
if (id == null)
{
return NotFound();
}

UserLibrary = await _context.UserLibraries
.Include(ul => ul.Game)
.Include(ul => ul.User)
.FirstOrDefaultAsync(m => m.Id == id);

if (UserLibrary == null)
var userLibrary = await _context.UserLibraries.AsNoTracking().FirstOrDefaultAsync(ul => ul.Id == id);
if (userLibrary == null)
{
return NotFound();
}

Games = new SelectList(_context.Games, "Id", "Title");
Users = new SelectList(_context.Users, "Id", "UserName");
// Populate the bound properties
SelectedId = userLibrary.Id;
SelectedUserId = userLibrary.UserId;
SelectedGameId = userLibrary.GameId;
SelectedStatus = userLibrary.Status;
SelectedIsUpcoming = userLibrary.IsUpcoming;

Games = new SelectList(await _context.Games.ToListAsync(), "Id", "Title");
Users = new SelectList(await _context.Users.ToListAsync(), "Id", "UserName");

return Page();
}
Expand All @@ -63,18 +77,32 @@ public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
Games = new SelectList(await _context.Games.ToListAsync(), "Id", "Title");
Users = new SelectList(await _context.Users.ToListAsync(), "Id", "UserName");
return Page();
}

_context.Attach(UserLibrary!).State = EntityState.Modified;
var userLibrary = await _context.UserLibraries.FindAsync(SelectedId);
if (userLibrary == null)
{
return NotFound();
}

// Update the entity with the new values
userLibrary.UserId = SelectedUserId;
userLibrary.GameId = SelectedGameId;
userLibrary.Status = SelectedStatus;
userLibrary.IsUpcoming = SelectedIsUpcoming;

_context.Attach(userLibrary).State = EntityState.Modified;

try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!UserLibraryExists(UserLibrary!.Id))
if (!UserLibraryExists(SelectedId))
{
return NotFound();
}
Expand Down
4 changes: 2 additions & 2 deletions GameLibrary/Pages/Admin/UserLibraries/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
{
<tr>
<td>
<strong style="color:#FFD43B;">User:</strong> @Html.DisplayFor(modelItem => item.UserId)<br />
<strong style="color:#FFD43B;">Game:</strong> @Html.DisplayFor(modelItem => item.GameId)<br />
<strong style="color:#FFD43B;">User:</strong> @Html.DisplayFor(modelItem => item.User.UserName)<br />
<strong style="color:#FFD43B;">Game:</strong> @Html.DisplayFor(modelItem => item.Game.Title)<br />
<strong style="color:#FFD43B;">Status:</strong> @Html.DisplayFor(modelItem => item.Status)<br />
<strong style="color:#FFD43B;">Is Upcoming:</strong> @Html.DisplayFor(modelItem => item.IsUpcoming)
</td>
Expand Down

0 comments on commit 9400d02

Please sign in to comment.