Skip to content

Commit

Permalink
fix: #dev async fixes 2
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinbator committed Nov 23, 2024
1 parent 5ab8bf9 commit 5c1150b
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 14 deletions.
5 changes: 0 additions & 5 deletions rag-2-backend/Infrastructure/Dao/CourseDao.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,4 @@ public virtual async Task<Course> GetCourseByIdOrThrow(int id)
return await dbContext.Courses.SingleOrDefaultAsync(u => u.Id == id) ??
throw new NotFoundException("Course not found");
}

public virtual async Task<List<Course>> GetAllCourses()
{
return await dbContext.Courses.ToListAsync();
}
}
1 change: 1 addition & 0 deletions rag-2-backend/Infrastructure/Dao/RefreshTokenDao.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class RefreshTokenDao(DatabaseContext context)
{
public virtual async Task RemoveTokensForUser(User user)
{
context.Attach(user);
var unusedTokens = await context.RefreshTokens.Where(r => r.User.Id == user.Id).ToListAsync();
context.RefreshTokens.RemoveRange(unusedTokens);
await context.SaveChangesAsync();
Expand Down
1 change: 1 addition & 0 deletions rag-2-backend/Infrastructure/Module/Auth/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public async Task LogoutUser(string token)
private async Task<RefreshToken> GenerateRefreshToken(double refreshTokenExpirationTimeDays,
Database.Entity.User user)
{
databaseContext.Attach(user);
var refreshToken = new RefreshToken
{
User = user,
Expand Down
4 changes: 2 additions & 2 deletions rag-2-backend/Infrastructure/Module/Course/CourseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ namespace rag_2_backend.Infrastructure.Module.Course;

public class CourseService(DatabaseContext context, CourseDao courseDao)
{
public async Task<IEnumerable<CourseResponse>> GetCourses()
public async Task<List<CourseResponse>> GetCourses()
{
var courses = await context.Courses.ToListAsync();

return courses.Select(CourseMapper.Map);
return courses.Select(CourseMapper.Map).ToList();
}

public async Task AddCourse(CourseRequest request)
Expand Down
5 changes: 1 addition & 4 deletions rag-2-backend/Infrastructure/Module/User/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ public async Task ChangePassword([Required] string oldPassword, [Required] strin
[Authorize]
public async Task DeleteAccount()
{
var header = HttpContext.Request.Headers.Authorization.FirstOrDefault() ??
throw new UnauthorizedAccessException("Unauthorized");

await userService.DeleteAccount(AuthDao.GetPrincipalEmail(User), header);
await userService.DeleteAccount(AuthDao.GetPrincipalEmail(User));
}
}
5 changes: 3 additions & 2 deletions rag-2-backend/Infrastructure/Module/User/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public async Task ChangePassword(string email, string oldPassword, string newPas
await context.SaveChangesAsync();
}

public async Task DeleteAccount(string email, string header)
public async Task DeleteAccount(string email)
{
var user = await userDao.GetUserByEmailOrThrow(email);

Expand All @@ -147,7 +147,6 @@ public async Task DeleteAccount(string email, string header)

context.Users.Remove(user);
await context.SaveChangesAsync();

await refreshTokenDao.RemoveTokensForUser(user);
}

Expand All @@ -157,6 +156,8 @@ private async Task UpdateUserProperties(
int? studyCycleYearA, int? studyCycleYearB, int? courseId, string? group, Database.Entity.User user
)
{
context.Attach(user);

if (user.Role == Role.Student && (
!studyCycleYearA.HasValue || !studyCycleYearB.HasValue || !courseId.HasValue ||
string.IsNullOrWhiteSpace(group))
Expand Down
2 changes: 1 addition & 1 deletion rag-2-backend/Test/Service/UserServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ await Assert.ThrowsAsync<BadRequestException>(() =>
[Fact]
public async Task ShouldDeleteAccount()
{
await _userService.DeleteAccount("[email protected]", "Bearer header");
await _userService.DeleteAccount("[email protected]");

_contextMock.Verify(c => c.Users.Remove(It.IsAny<User>()), Times.Once);
}
Expand Down

0 comments on commit 5c1150b

Please sign in to comment.