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

Feature/show ratings in profile #165

Merged
merged 5 commits into from
Sep 22, 2024
Merged
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
57 changes: 47 additions & 10 deletions Project/Project/Controllers/MoviesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,33 +312,39 @@ public async Task<IActionResult> AddRating(string movieId, string poster, string

if (existingRating != null)
{
UpdateExistingRating(existingRating, ratingValue);
UpdateRating(existingRating, ratingValue);
}
else
{
await AddNewRating(movieId, poster, title, ratingValue, currentUser.Id);
await StoreRating(movieId, poster, title, ratingValue, currentUser.Id);
}

await _context.SaveChangesAsync();

TempData["SuccessMessage"] = $"You've successfully rated {title}!";
return Redirect(Request.Headers["Referer"].ToString());

return Json(new
{
success = true,
ratingValue,
message = $"You've successfully rated {title}!",
});
}
else
{
var originalUrl = Request.Headers["Referer"].ToString();
return RedirectToAction("Login", "Auth", new { returnUrl = originalUrl });

return Json(new
{
success = false,
message = "You must be logged in to rate a movie.",
});
}
}

private void UpdateExistingRating(Rating existingRating, int ratingValue)
private void UpdateRating(Rating existingRating, int ratingValue)
{
existingRating.Rating_value = ratingValue;
existingRating.Updated_at = DateTime.Now;
}

private async Task AddNewRating(string movieId, string poster, string title, int ratingValue, int userId)
private async Task StoreRating(string movieId, string poster, string title, int ratingValue, int userId)
{
var rating = new Rating
{
Expand All @@ -353,5 +359,36 @@ private async Task AddNewRating(string movieId, string poster, string title, int

await _context.Ratings.AddAsync(rating);
}

public async Task<IActionResult> DeleteRating(string movieId)
{
var userJson = HttpContext.Session.GetString("CurrentUser");

if (userJson != null)
{
var currentUser = JsonConvert.DeserializeObject<User>(userJson);

if (movieId == null)
{
return View("NotFound");
}

var rating = _context.Ratings.FirstOrDefault(rating => rating.Movie_id == movieId && rating.User_id == currentUser.Id);

if (rating == null)
{
return View("NotFound");
}

_context.Ratings.Remove(rating);
await _context.SaveChangesAsync();

return Json(new { success = true, message = "Rating has been successfully deleted" });
}
else
{
return Json(new { success = false, redirectToLogin = true, message = "User not logged in." });
}
}
}
}
4 changes: 4 additions & 0 deletions Project/Project/Controllers/ProfileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public async Task<IActionResult> Index()
var reviews = await _context.Reviews
.Where(movie => movie.User_id == currentUser.Id)
.ToListAsync();
var ratings = await _context.Ratings
.Where(movie => movie.User_id == currentUser.Id)
.ToListAsync();

var combinedItems = new List<IMovieItem>();
combinedItems.AddRange(favorites);
Expand All @@ -48,6 +51,7 @@ public async Task<IActionResult> Index()
Watchlist = watchlist.Cast<IMovieItem>().ToList(),
Recommendations = recommendation.Cast<IMovieItem>().ToList(),
Reviews = reviews,
Ratings = ratings,
};

return View(viewModel);
Expand Down
1 change: 1 addition & 0 deletions Project/Project/Models/ViewModels/ProfileViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public class ProfileViewModel
public List<IMovieItem> Watchlist { get; set; }
public List<IMovieItem> Recommendations { get; set; }
public List<Review> Reviews { get; set; }
public List<Rating> Ratings { get; set; }
}
}
10 changes: 10 additions & 0 deletions Project/Project/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,14 @@
pattern: "how-to-use/",
defaults: new { controller = "HowToUse", action = "Index" });

app.MapControllerRoute(
name: "update_rating",
pattern: "content/{id?}/update-rating",
defaults: new { controller = "Movies", action = "AddRating" });

app.MapControllerRoute(
name: "delete_rating",
pattern: "content/{id?}/delete-rating",
defaults: new { controller = "Movies", action = "DeleteRating" });

app.Run();
8 changes: 5 additions & 3 deletions Project/Project/Views/Movies/Show.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@
<div>
@await Html.PartialAsync("_RateMovieModal",
Tuple.Create(Model.Movie.Id, Model.Movie.Title, Model.Movie.Poster, Model.IsRated ? "Your Rating" : "Rate " + @Model.Movie.Type, Model.IsRated ? "fa-solid fa-pen-to-square" : "fas fa-plus", "btn-without text-light"))
<h6 style="margin: 0">
<h6 class="rating-h6" style="margin: 0">
@if (Model.UserHasRating)
{
<i class="fa-solid fa-star" style="color: #FFD43B;"></i>
<span class="rate">@Model.CurrentUserRating /10</span>
<span class="rate" id="[email protected]">@Model.CurrentUserRating /10</span>
}
</h6>
</div>
Expand Down Expand Up @@ -202,7 +202,9 @@
Model.Movie.Id,
"/content/@Model.Movie.Id/delete-review-from-movie-page",
"Are you sure you want to delete your review?",
"Delete", ""))
"Delete",
Tuple.Create("btn-sm", ""),
"show"))
</div>
}
</div>
Expand Down
3 changes: 2 additions & 1 deletion Project/Project/Views/Profile/Index.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@

@await Html.PartialAsync("_ProfileTabButton",
Tuple.Create(
Tuple.Create("Watchlist", "Favorites", "Recommendations", "Reviews"),
Tuple.Create("Watchlist", "Favorites", "Recommendations", "Ratings","Reviews"),
await Html.PartialAsync("_ProfileMovieListComponent", Tuple.Create(Model.Watchlist, true, "/Movies/DestroyWatchlistMovieFromProfile")),
await Html.PartialAsync("_ProfileMovieListComponent", Tuple.Create(Model.Favorites, true, "/Movies/DestroyFavoriteMovieFromProfile")),
await Html.PartialAsync("_ProfileMovieListComponent", Tuple.Create(Model.Recommendations, false, "")),
await Html.PartialAsync("_RatingListComponent", Tuple.Create(Model.Ratings, true, "")),
await Html.PartialAsync("_ProfileReviews", Model.Reviews)
))
</section>
22 changes: 13 additions & 9 deletions Project/Project/Views/Shared/_DeleteConfirmationModal.cshtml
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
@model Tuple<string, string, string, string, string>
@model Tuple<string, string, string, string, Tuple<string, string>, string>

@{
var movieId = Model.Item1;
var formAction = Model.Item2;
var deleteMessage = Model.Item3;
var btnText = Model.Item4;
var customClass = Model.Item5;
var btnDeleteClassStyle = Model.Item5.Item1;
var btnFormClass = Model.Item5.Item2;
var profileTab = Model.Item6;
}

@await Html.PartialAsync("_PrimaryButton", Tuple.Create(
btnText,
"",
"btn btn-red btn-sm",
"type=button data-toggle=modal data-target=#deleteConfirmationModal",
$"btn btn-red {btnDeleteClassStyle}",
$"type=button data-toggle=modal data-target=#deleteConfirmationModal-{profileTab}-{movieId}",
"button")
)

<div class="modal" id="deleteConfirmationModal">
<div class="modal" id="deleteConfirmationModal-@profileTab-@movieId">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-body">
<h6 class="text-dark">@deleteMessage</h6>
<form action="@formAction" method="post">
<input type="hidden" name="movieId" value="@movieId" />
@Html.AntiForgeryToken()

<input type="hidden" name="movieId" value="@movieId"/>
<div class="modal-footer">
@await Html.PartialAsync("_PrimaryButton",
Tuple.Create(
Expand All @@ -31,16 +35,16 @@
"btn btn-secondary",
"type=button data-dismiss=modal",
"button")
)
)

@await Html.PartialAsync("_PrimaryButton",
Tuple.Create(
"Delete",
"",
$"btn btn-red {customClass}",
$"btn btn-red {btnFormClass}",
"type=submit",
"button")
)
)
</div>
</form>
</div>
Expand Down
5 changes: 3 additions & 2 deletions Project/Project/Views/Shared/_ProfileReviews.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
@foreach (var review in Model)
{
<div class="d-flex flex-column flex-md-row position-relative w-50 mb-3 review-container">
<div class="d-flex flex-column flex-md-row position-relative w-50 mb-3 tab-content">
<div class="card review-card mb-3 col-12 col-md-6 p-0 w-100 h-100 d-flex flex-column" style="max-width: 540px;">
<div class="row g-0 h-100 bg-purple">
<div class="col-md-4">
Expand Down Expand Up @@ -45,7 +45,8 @@
"/content/@Model.Movie.Id/delete-review-from-profile",
"Are you sure you want to delete your review?",
"Delete",
"delete-confirmation-btn"))
Tuple.Create("btn-sm", "delete-confirmation-btn"),
"review"))
</div>
</div>
</div>
Expand Down
11 changes: 8 additions & 3 deletions Project/Project/Views/Shared/_ProfileTabButton.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ await Html.PartialAsync("_OtherPartial", Tuple.Create(...))

<div>
@model Tuple<
Tuple<string, string, string, string>,
Tuple<string, string, string, string, string>,
Microsoft.AspNetCore.Html.IHtmlContent,
Microsoft.AspNetCore.Html.IHtmlContent,
Microsoft.AspNetCore.Html.IHtmlContent,
Microsoft.AspNetCore.Html.IHtmlContent,
Expand All @@ -29,15 +30,19 @@ await Html.PartialAsync("_OtherPartial", Tuple.Create(...))
<button class="nav-link" id="recommendations-tab" data-bs-toggle="tab" data-bs-target="#recommendations" type="button" role="tab" aria-controls="contact" aria-selected="false">@Model.Item1.Item3</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="reviews-tab" data-bs-toggle="tab" data-bs-target="#reviews" type="button" role="tab" aria-controls="contact" aria-selected="false">@Model.Item1.Item4</button>
<button class="nav-link" id="ratings-tab" data-bs-toggle="tab" data-bs-target="#ratings" type="button" role="tab" aria-controls="contact" aria-selected="false">@Model.Item1.Item4</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="reviews-tab" data-bs-toggle="tab" data-bs-target="#reviews" type="button" role="tab" aria-controls="contact" aria-selected="false">@Model.Item1.Item5</button>
</li>
</ul>
<div class="bg-secondary-color">
<div class="tab-content pt-5 pb-5 container" id="myTabContent">
<div class="tab-pane fade show active" id="watchlist" role="tabpanel" aria-labelledby="watchlist-tab">@Model.Item2</div>
<div class="tab-pane fade" id="favorites" role="tabpanel" aria-labelledby="favorites-tab">@Model.Item3</div>
<div class="tab-pane fade" id="recommendations" role="tabpanel" aria-labelledby="recommendations-tab">@Model.Item4</div>
<div class="tab-pane fade" id="reviews" role="tabpanel" aria-labelledby="reviews-tab">@Model.Item5</div>
<div class="tab-pane fade" id="ratings" role="tabpanel" aria-labelledby="ratings-tab">@Model.Item5</div>
<div class="tab-pane fade" id="reviews" role="tabpanel" aria-labelledby="reviews-tab">@Model.Item6</div>
</div>
</div>
</div>
53 changes: 27 additions & 26 deletions Project/Project/Views/Shared/_RateMovieModal.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,63 @@
}

<!-- Button to trigger the modal -->
<button type="button" class="@buttonClass" data-toggle="modal" data-target="#exampleModalCenter">
<button type="button" class="@buttonClass" data-toggle="modal" data-target="#ratingModalCenter-@movieId">
@buttonText
<i class="@buttonIcon"></i >
<i class="@buttonIcon"></i>
</button>

<!-- Modal Structure -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal fade" id="ratingModalCenter-@movieId" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="d-flex justify-content-between flex-column">
<div class="modal-header">
<div>
@if (!string.IsNullOrEmpty(Context.Session.GetString("CurrentUser")))
{
<h5 class="modal-title text-dark" id="exampleModalLongTitle">@movieTitle</h5>
<h5 class="modal-title text-dark" id="exampleModalLongTitle">@movieTitle</h5>
}
else
{
<h5 class="modal-title text-dark" id="exampleModalLongTitle">Sign in in to leave your rating</h5>
<h5 class="modal-title text-dark" id="exampleModalLongTitle">Sign in to leave your rating</h5>
}
</div>
<div>
@await Html.PartialAsync("_PrimaryButton", Tuple.Create("X", "", "btn-without pe-auto", "type=button data-dismiss=modal", "button"))
</div>
</div>

<div class="modal-body">
@if (!string.IsNullOrEmpty(Context.Session.GetString("CurrentUser")))
{
<div class="rating text-center" id="ratingContainer">
<form asp-action="AddRating" method="post" class="d-flex align-items-center" id="ratingForm">
@Html.AntiForgeryToken()
<div>
<input type="hidden" name="movieId" value="@movieId" />
<input type="hidden" name="title" value="@movieTitle" />
<input type="hidden" name="poster" value="@moviePoster" />
<input type="hidden" name="ratingValue" id="ratingValue" value="0" />
</div>
</form>
</div>
<div class="rating text-center" id="ratingContainer-@movieId">
<form action="/content/@movieId/update-rating" method="post" class="d-flex align-items-center" id="ratingForm-@movieId">
@Html.AntiForgeryToken()
<div>
<input type="hidden" name="movieId" value="@movieId" />
<input type="hidden" name="title" value="@movieTitle" />
<input type="hidden" name="poster" value="@moviePoster" />
<input type="hidden" name="ratingValue" id="ratingValue-@movieId" value="0" />
</div>
</form>
</div>
}
else
{
@await Html.PartialAsync("_PrimaryButton",
Tuple.Create(
"Close",
"",
"btn btn-secondary",
"type=button data-dismiss=modal",
"button")
)
@await Html.PartialAsync("_PrimaryButton", Tuple.Create("Sign in", "style-1", "", $"href={returnUrl}", "a"))
@await Html.PartialAsync("_PrimaryButton",
Tuple.Create(
"Close",
"",
"btn btn-secondary",
"type=button data-dismiss=modal",
"button")
)
@await Html.PartialAsync("_PrimaryButton", Tuple.Create("Sign in", "style-1", "", $"href={returnUrl}", "a"))
}
</div>
</div>
</div>
</div>
</div>


Loading
Loading