-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from TomasSirotek/feature/course-like
🛠 Feature: As a user I need to be able to like/save courses and displ…
- Loading branch information
Showing
37 changed files
with
1,784 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/Application/Features/Courses/Commands/AddCourseUserWL/AddCourseUserWL.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using SkillSphere.Application.Common.Interfaces; | ||
using SkillSphere.Domain.Entities; | ||
|
||
namespace SkillSphere.Application.Features.Courses.Commands.AddCourseUserWL; | ||
|
||
public record AddCourseUserWLCommand(Guid UserId, Guid CourseId) : IRequest; | ||
|
||
public class AddCourseUserWLCommandHandler : IRequestHandler<AddCourseUserWLCommand> | ||
{ | ||
private readonly IApplicationDbContext _context; | ||
|
||
public AddCourseUserWLCommandHandler(IApplicationDbContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public async Task Handle(AddCourseUserWLCommand request, CancellationToken cancellationToken) | ||
{ | ||
var newWishlistItem = new WishListItem { UserId = request.UserId, CourseId = request.CourseId, }; | ||
|
||
_context.WishlistItems.Add(newWishlistItem); | ||
await _context.SaveChangesAsync(cancellationToken); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Application/Features/Courses/Commands/AddCourseUserWL/CourseWishListCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace SkillSphere.Application.Features.Courses.Commands.AddCourseUserWL; | ||
|
||
public class CourseWishListCommand : IRequest | ||
{ | ||
public Guid CourseId { get; init; } | ||
public Guid UserId { get; init; } | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Application/Features/Courses/Commands/DecreaseLike/DecreaseLike.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using SkillSphere.Application.Common.Interfaces; | ||
|
||
namespace SkillSphere.Application.Features.Courses.Commands.DecreaseLike; | ||
|
||
public record DecreaseLikeCountCommand(Guid CourseId) : IRequest; | ||
|
||
public class DecreaseLikeCountCommandHandler : IRequestHandler<DecreaseLikeCountCommand> | ||
{ | ||
private readonly IApplicationDbContext _context; | ||
|
||
public DecreaseLikeCountCommandHandler(IApplicationDbContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public async Task Handle(DecreaseLikeCountCommand request, CancellationToken cancellationToken) | ||
{ | ||
var course = await _context.Courses.FindAsync(request.CourseId); | ||
|
||
Guard.Against.NotFound(request.CourseId, course); | ||
|
||
if (course.Likes > 0) | ||
{ | ||
course.Likes--; | ||
await _context.SaveChangesAsync(cancellationToken); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/Application/Features/Courses/Commands/IncreaseLikes/IncreaseLikes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using SkillSphere.Application.Common.Interfaces; | ||
|
||
namespace SkillSphere.Application.Features.Courses.Commands.IncreaseLikes; | ||
|
||
public record IncreaseLikesCommand(Guid CourseId) : IRequest; | ||
|
||
public class IncreaseLikesCommandHandler : IRequestHandler<IncreaseLikesCommand> | ||
{ | ||
private readonly IApplicationDbContext _context; | ||
|
||
public IncreaseLikesCommandHandler(IApplicationDbContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public async Task Handle(IncreaseLikesCommand request, CancellationToken cancellationToken) | ||
{ | ||
var course = await _context.Courses.FindAsync(request.CourseId); | ||
|
||
Guard.Against.NotFound(request.CourseId, course); | ||
|
||
course.Likes++; | ||
await _context.SaveChangesAsync(cancellationToken); | ||
} | ||
} | ||
|
30 changes: 30 additions & 0 deletions
30
src/Application/Features/Courses/Commands/RemoveCourseWL/RemoveCourseWL.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using SkillSphere.Application.Common.Interfaces; | ||
using SkillSphere.Application.TodoLists.Commands.DeleteTodoList; | ||
|
||
namespace SkillSphere.Application.Features.Courses.Commands.RemoveCourseWL; | ||
|
||
public record RemoveCourseWLCommand(Guid CourseId,Guid UserId) : IRequest; | ||
|
||
public class RemoveCourseWLCommandHandler : IRequestHandler<RemoveCourseWLCommand> | ||
{ | ||
private readonly IApplicationDbContext _context; | ||
|
||
public RemoveCourseWLCommandHandler(IApplicationDbContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public async Task Handle(RemoveCourseWLCommand request, CancellationToken cancellationToken) | ||
{ | ||
var wishlistItem = await _context.WishlistItems | ||
.Where(l => l.UserId == request.UserId && l.CourseId == request.CourseId) | ||
.SingleOrDefaultAsync(cancellationToken); | ||
|
||
Guard.Against.NotFound(request.CourseId, wishlistItem); | ||
|
||
_context.WishlistItems.Remove(wishlistItem); | ||
|
||
await _context.SaveChangesAsync(cancellationToken); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/Application/Features/Courses/Queries/GetUserWishList/GetUserWishListQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using SkillSphere.Application.Common.Interfaces; | ||
using SkillSphere.Application.Features.Courses.Queries.GetAllCourses; | ||
|
||
namespace SkillSphere.Application.Features.Courses.Queries; | ||
|
||
public class GetUserWishListQuery : IRequest<GetCourseVm> | ||
{ | ||
public Guid UserId { get; init; } | ||
} | ||
|
||
|
||
public class GetUserWishListQueryHandler : IRequestHandler<GetUserWishListQuery, GetCourseVm> | ||
{ | ||
private readonly IApplicationDbContext _context; | ||
private readonly IMapper _mapper; | ||
|
||
public GetUserWishListQueryHandler(IApplicationDbContext context, IMapper mapper) | ||
{ | ||
_context = context; | ||
_mapper = mapper; | ||
} | ||
|
||
public async Task<GetCourseVm> Handle(GetUserWishListQuery request, CancellationToken cancellationToken) | ||
{ | ||
|
||
Guid userId = request.UserId; | ||
|
||
|
||
|
||
return new GetCourseVm | ||
{ | ||
Courses = await _context.Courses | ||
.AsNoTracking() | ||
.Include(c => c.Categories) | ||
.Include(c => c.Chapters) | ||
.Where(c => c.WishList.Any(wi => wi.UserId == userId)) | ||
.ProjectTo<QueryDto>(_mapper.ConfigurationProvider) | ||
.OrderBy(t => t.Title) | ||
.ToListAsync(cancellationToken) | ||
}; | ||
|
||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace SkillSphere.Domain.Entities; | ||
|
||
public class WishListItem | ||
{ | ||
public Guid UserId { get; set; } | ||
public ApplicationUser? User { get; set; } | ||
|
||
public Guid CourseId { get; set; } | ||
public Course? Course { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Infrastructure/Data/Configurations/WishListItemConfiguration.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Metadata.Builders; | ||
using SkillSphere.Domain.Entities; | ||
|
||
namespace SkillSphere.Infrastructure.Data.Configurations; | ||
|
||
public class WishListItemConfiguration : IEntityTypeConfiguration<WishListItem> | ||
{ | ||
public void Configure(EntityTypeBuilder<WishListItem> builder) | ||
{ | ||
builder | ||
.HasKey(wi => new { wi.UserId, wi.CourseId }); | ||
|
||
builder | ||
.HasOne(wi => wi.User) | ||
.WithMany(u => u.WishList) | ||
.HasForeignKey(wi => wi.UserId); | ||
|
||
builder | ||
.HasOne(wi => wi.Course) | ||
.WithMany(c => c.WishList) | ||
.HasForeignKey(wi => wi.CourseId); | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
Oops, something went wrong.