Skip to content

Commit

Permalink
backend homework changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrohim-qosimov committed Sep 14, 2024
1 parent 8ccc993 commit db4fe07
Show file tree
Hide file tree
Showing 25 changed files with 670 additions and 73 deletions.
30 changes: 27 additions & 3 deletions UrphaCapital.API/Controllers/HomeworksController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using UrphaCapital.Application.UseCases.Homework.Queries;
using UrphaCapital.Application.UseCases.Lessons.Queries;
using UrphaCapital.Application.ViewModels;
using UrphaCapital.Domain.DTOs;
using UrphaCapital.Domain.Entities;

namespace UrphaCapital.API.Controllers
Expand All @@ -25,7 +26,7 @@ public async Task<ResponseModel> PostLesson([FromForm] CreateHomeworkCommand com

return response;
}

[HttpDelete("{id}")]
public async Task<ResponseModel> RemoveHomework(long id, CancellationToken cancellation)
{
Expand All @@ -35,15 +36,38 @@ public async Task<ResponseModel> RemoveHomework(long id, CancellationToken cance

return response;
}

[HttpPut]
public async Task<ResponseModel> PutHomework([FromForm] UpdateHomeworkCommand command, CancellationToken cancellation)
{
var response = await _mediator.Send(command, cancellation);

return response;
}


[HttpPut("grade-homework")]
public async Task<ResponseModel> PutHomework([FromBody] GradeHomeworkCommand command, CancellationToken cancellation)
{
var response = await _mediator.Send(command, cancellation);

return response;
}

[HttpGet("{studentId}/results/{index}/{count}")]
public async Task<IEnumerable<HomeworkResultView>> GetStudentHomeworkResults(long studentId, int index, int count, CancellationToken cancellation)
{
var query = new GetStudentHomeworkResultsQuery()
{
StudentId = studentId,
Index = index,
Count = count,
};

var response = await _mediator.Send(query, cancellation);

return response;
}

[HttpGet("{index}/{count}")]
public async Task<IEnumerable<Homeworks>> GetAll(int index, int count, CancellationToken cancellation)
{
Expand Down
6 changes: 3 additions & 3 deletions UrphaCapital.Application/AuthServices/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public TokenModel GenerateToken(Student user)
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.Iat, EpochTime.GetIntDate(DateTime.UtcNow).ToString(CultureInfo.InvariantCulture), ClaimValueTypes.Integer64),
new Claim("UserId", user.Id.ToString()),
new Claim("Name", user.FullName),
new Claim("Title", user.FullName),
new Claim("Phone", user.PhoneNumber),
new Claim("ids", ids),
new Claim("Address", user.Address),
Expand Down Expand Up @@ -75,7 +75,7 @@ public TokenModel GenerateToken(Admin user)
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.Iat, EpochTime.GetIntDate(DateTime.UtcNow).ToString(CultureInfo.InvariantCulture), ClaimValueTypes.Integer64),
new Claim("Id", user.Id.ToString()),
new Claim("Name", user.Name),
new Claim("Title", user.Name),
new Claim("Phone", user.PhoneNumber),
new Claim("Email", user.Email),
new Claim("Role", user.Role!)
Expand Down Expand Up @@ -111,7 +111,7 @@ public TokenModel GenerateToken(Mentor user)
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()),
new Claim(JwtRegisteredClaimNames.Iat, EpochTime.GetIntDate(DateTime.UtcNow).ToString(CultureInfo.InvariantCulture), ClaimValueTypes.Integer64),
new Claim("Id", user.Id.ToString()),
new Claim("Name", user.Name),
new Claim("Title", user.Name),
new Claim("Phone", user.PhoneNumber),
new Claim("Email", user.Email),
new Claim("Role", user.Role!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CreateHomeworkCommand: IRequest<ResponseModel>
public string Title { get; set; }
public IFormFile FILE { get; set; }
public string Description { get; set; }
public long studentId { get; set; }
public long StudentId { get; set; }
public Guid LessonId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UrphaCapital.Application.ViewModels;

namespace UrphaCapital.Application.UseCases.Homework.Commands
{
public class GradeHomeworkCommand : IRequest<ResponseModel>
{
public long HomeworkId { get; set; }
public long MentorId { get; set; }
public int Grade { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class UpdateHomeworkCommand: IRequest<ResponseModel>
public string Title { get; set; }
public IFormFile FILE { get; set; }
public string Description { get; set; }
public long studentId { get; set; }
public long StudentId { get; set; }
public Guid LessonId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public async Task<ResponseModel> Handle(CreateHomeworkCommand request, Cancellat
Title = request.Title,
LessonId = request.LessonId,
Description = request.Description,
studentId = request.studentId,
StudentId = request.StudentId,
FILE = "/HomeworkFile/" + fileName,
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UrphaCapital.Application.Abstractions;
using UrphaCapital.Application.UseCases.Homework.Commands;
using UrphaCapital.Application.ViewModels;

namespace UrphaCapital.Application.UseCases.Homework.Handlers
{
public class GradeHomeworkCommandHandler : IRequestHandler<GradeHomeworkCommand, ResponseModel>
{
private readonly IApplicationDbContext _context;
public async Task<ResponseModel> Handle(GradeHomeworkCommand request, CancellationToken cancellationToken)
{
var homework = await _context.Homeworks.FindAsync(request.HomeworkId);
if (homework == null) return new ResponseModel() { IsSuccess = false, Message = "Not Found", StatusCode = 404 };

homework.Grade = request.Grade;
homework.MentorId = request.MentorId;
await _context.SaveChangesAsync();

return new ResponseModel()
{
IsSuccess = true,
Message = "Successfully Graded",
StatusCode = 200
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async Task<ResponseModel> Handle(UpdateHomeworkCommand request, Cancellat
hw.Title = request.Title;
hw.FILE = "/HomeworkFile/" + fileName;
hw.Description = request.Description;
hw.studentId = request.studentId;
hw.StudentId = request.StudentId;
hw.LessonId = request.LessonId;

await _context.SaveChangesAsync(cancellationToken);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UrphaCapital.Domain.DTOs;

namespace UrphaCapital.Application.UseCases.Homework.Queries
{
public class GetStudentHomeworkResultsQuery : IRequest<IEnumerable<HomeworkResultView>>
{
public long StudentId { get; set; }
public int Index { get; set; }
public int Count { get; set; }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UrphaCapital.Application.Abstractions;
using UrphaCapital.Application.UseCases.Homework.Queries;
using UrphaCapital.Domain.DTOs;
using UrphaCapital.Domain.Entities.Auth;

namespace UrphaCapital.Application.UseCases.Homework.QueriesHandler
{
public class GetStudentHomeworkResultsQueryHandler : IRequestHandler<GetStudentHomeworkResultsQuery, IEnumerable<HomeworkResultView>>
{
private readonly IApplicationDbContext _context;

public GetStudentHomeworkResultsQueryHandler(IApplicationDbContext context)
{
_context = context;
}

public async Task<IEnumerable<HomeworkResultView>> Handle(GetStudentHomeworkResultsQuery request, CancellationToken cancellationToken)
{
var homeworkList = await _context.Homeworks
.Where(h => h.StudentId == request.StudentId)
.Skip(request.Index - 1)
.Take(request.Count)
.Include(h => h.Lesson)
.ToListAsync();

var result = homeworkList.Select(h => new HomeworkResultView()
{
HomeworkId = h.Id,
FILE = h.FILE,
Grade = h.Grade,
LessonTitle = h.Lesson.Title
});

return result;

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class CreateLessonCommand : IRequest<ResponseModel>
{
public string Name { get; set; }
public Guid CourseId { get; set; }
public string HomeworkDescription { get; set; }
public IFormFile Video { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public class UpdateLessonCommand : IRequest<ResponseModel>
public string Name { get; set; }
public Guid CourseId { get; set; }
public IFormFile Video { get; set; }
public string HomeworkDescription { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ public async Task<ResponseModel> Handle(CreateLessonCommand request, Cancellatio

var category = new Lesson()
{
Name = request.Name,
Title = request.Name,
CourseId = request.CourseId,
HomeworkDescription = request.HomeworkDescription,
Video = "/LessonVideos/" + fileName,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ public async Task<ResponseModel> Handle(UpdateLessonCommand request, Cancellatio



lesson.Name = request.Name;
lesson.Title = request.Name;
lesson.Video = "/LessonVideos/" + fileName;
lesson.CourseId = request.CourseId;
lesson.HomeworkDescription = request.HomeworkDescription;

await _context.SaveChangesAsync(cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task<IEnumerable<Lesson>> Handle(GetAllLessonsByCourseIdQuery reque
.Select(x => new Lesson
{
Id = x.Id,
Name = x.Name,
Title = x.Title,
CourseId = x.CourseId
})
.ToListAsync(cancellationToken),
Expand Down
16 changes: 16 additions & 0 deletions UrphaCapital.Domain/DTOs/HomeworkResultView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UrphaCapital.Domain.DTOs
{
public class HomeworkResultView
{
public long HomeworkId { get; set; }
public string LessonTitle { get; set; }
public string FILE { get; set; }
public int? Grade { get; set; }
}
}
5 changes: 4 additions & 1 deletion UrphaCapital.Domain/Entities/Homeworks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ public class Homeworks
public string Title { get; set; }
public string FILE { get; set; }
public string Description { get; set; }
public long StudentId { get; set; }
public int? Grade { get; set; }
public long? MentorId { get; set; }
public Guid LessonId { get; set; }
public long studentId { get; set; }

public Lesson Lesson { get; set; }
}
}
7 changes: 5 additions & 2 deletions UrphaCapital.Domain/Entities/Lesson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ namespace UrphaCapital.Domain.Entities
public class Lesson
{
public Guid Id { get; set; } = Guid.NewGuid();
public string Name { get; set; }
public string Title { get; set; }
public string HomeworkDescription { get; set; }
public string Video { get; set; }
public Guid CourseId { get; set; }


public Course Course { get; set; }
public string Video { get; set; }
}
}
Loading

0 comments on commit db4fe07

Please sign in to comment.