Skip to content

Commit

Permalink
backend ancha muncha changelar
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrohim-qosimov committed Sep 12, 2024
1 parent 88f680b commit 8ccc993
Show file tree
Hide file tree
Showing 47 changed files with 144 additions and 123 deletions.
2 changes: 1 addition & 1 deletion UrphaCapital.API/Controllers/AdminsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public async Task<ResponseModel> RemoveAdmin(long id, CancellationToken cancella

[HttpPost("Login")]
[EnableRateLimiting("sliding")]
public async Task<string> Login([FromBody] AdminLogin loginModel, CancellationToken cancellation)
public async Task<TokenModel> Login([FromBody] AdminLogin loginModel, CancellationToken cancellation)
{
if (ModelState.IsValid == false)
{
Expand Down
4 changes: 2 additions & 2 deletions UrphaCapital.API/Controllers/CoursesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task<ResponseModel> Create([FromForm] CreateCourseCommand command,
}

[HttpGet("GetById/{id}")]
public async Task<Course> GetById(long id, CancellationToken cancellation)
public async Task<Course> GetById(string id, CancellationToken cancellation)
{
var query = new GetCourseByIdQuery { Id = id };

Expand Down Expand Up @@ -74,7 +74,7 @@ public async Task<ResponseModel> Update([FromForm] UpdateCourseCommand command,
}

[HttpDelete("{id}")]
public async Task<ResponseModel> Delete(long id, CancellationToken cancellation)
public async Task<ResponseModel> Delete(string id, CancellationToken cancellation)
{
var command = new DeleteCourseCommand { Id = id };

Expand Down
8 changes: 4 additions & 4 deletions UrphaCapital.API/Controllers/LessonsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task<ResponseModel> PostLesson([FromForm] CreateLessonCommand comma
}

[HttpGet("{id}")]
public async Task<Lesson> GetLessonById(long id, CancellationToken cancellation)
public async Task<Lesson> GetLessonById(string id, CancellationToken cancellation)
{
var query = new GetLessonByIdQuery { Id = id };

Expand All @@ -39,7 +39,7 @@ public async Task<Lesson> GetLessonById(long id, CancellationToken cancellation)
}

[HttpGet("getvideo")]
public async Task<IActionResult> GetLessonVideo([FromQuery] long lessonId, CancellationToken cancellation)
public async Task<IActionResult> GetLessonVideo([FromQuery] string lessonId, CancellationToken cancellation)
{
var query = new GetLessonVideoQuery { Id = lessonId };

Expand All @@ -58,7 +58,7 @@ public async Task<IActionResult> GetLessonVideo([FromQuery] long lessonId, Cance
}

[HttpGet("{courseId}/{index}/{count}")]
public async Task<IEnumerable<Lesson>> GetLessonsByCourseId(int index, int count, long courseId, CancellationToken cancellation)
public async Task<IEnumerable<Lesson>> GetLessonsByCourseId(string courseId, int index, int count, CancellationToken cancellation)
{
var query = new GetAllLessonsByCourseIdQuery()
{
Expand All @@ -81,7 +81,7 @@ public async Task<ResponseModel> PutLesson([FromForm] UpdateLessonCommand comman
}

[HttpDelete("{id}")]
public async Task<ResponseModel> RemoveLesson(long id, CancellationToken cancellation)
public async Task<ResponseModel> RemoveLesson(string id, CancellationToken cancellation)
{
var command = new DeleteLessonCommand { Id = id };

Expand Down
2 changes: 1 addition & 1 deletion UrphaCapital.API/Controllers/MentorsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public async Task<ResponseModel> Delete(long id, CancellationToken cancellation)

[HttpPost("Login")]
[EnableRateLimiting("sliding")]
public async Task<string> Login([FromBody] MentorLogin loginModel, CancellationToken cancellation)
public async Task<TokenModel> Login([FromBody] MentorLogin loginModel, CancellationToken cancellation)
{
if (ModelState.IsValid == false)
{
Expand Down
4 changes: 2 additions & 2 deletions UrphaCapital.API/Controllers/StudentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async Task<ResponseModel> PutStudent([FromBody] UpdateStudentCommand comm
}

[HttpDelete("{id}")]
public async Task<ResponseModel> RemoveStudent(long id, CancellationToken cancellation)
public async Task<ResponseModel> RemoveStudent(string id, CancellationToken cancellation)
{
var command = new DeleteLessonCommand { Id = id };

Expand All @@ -78,7 +78,7 @@ public async Task<ResponseModel> RemoveStudent(long id, CancellationToken cancel

[HttpPost("Login")]
[EnableRateLimiting("sliding")]
public async Task<string> Login([FromBody] AdminLogin loginModel, CancellationToken cancellation)
public async Task<TokenModel> Login([FromBody] AdminLogin loginModel, CancellationToken cancellation)
{
if (ModelState.IsValid == false)
{
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 44 additions & 21 deletions UrphaCapital.Application/AuthServices/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using UrphaCapital.Application.ViewModels;
using UrphaCapital.Domain.Entities.Auth;

namespace UrphaCapital.Application.AuthServices
Expand All @@ -17,7 +18,7 @@ public AuthService(IConfiguration configuration)
_configuration = configuration;
}

public string GenerateToken(Student user)
public TokenModel GenerateToken(Student user)
{
SymmetricSecurityKey securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWTSettings:Secret"]!));
SigningCredentials credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
Expand All @@ -35,12 +36,13 @@ public string 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(ClaimTypes.Name, user.FullName),
new Claim(ClaimTypes.MobilePhone, user.PhoneNumber),
new Claim("Name", user.FullName),
new Claim("Phone", user.PhoneNumber),
new Claim("ids", ids),
new Claim(ClaimTypes.StreetAddress, user.Address),
new Claim(ClaimTypes.Email, user.Email),
new Claim(ClaimTypes.Role, user.Role!)
new Claim("Address", user.Address),
new Claim("Email", user.Email),
new Claim("Role", user.Role!),
new Claim(ClaimTypes.UserData, ids)
};


Expand All @@ -52,10 +54,17 @@ public string GenerateToken(Student user)
expires: DateTime.UtcNow.AddMinutes(expirePeriod),
signingCredentials: credentials);

return new JwtSecurityTokenHandler().WriteToken(token);
var response = new TokenModel()
{
Token = new JwtSecurityTokenHandler().WriteToken(token),
isSuccess = true,
Message = "Token generated successfully!"
};

return response;
}

public string GenerateToken(Admin user)
public TokenModel GenerateToken(Admin user)
{
SymmetricSecurityKey securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWTSettings:Secret"]!));
SigningCredentials credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
Expand All @@ -65,11 +74,11 @@ public string 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("UserId", user.Id.ToString()),
new Claim(ClaimTypes.Name, user.Name),
new Claim(ClaimTypes.MobilePhone, user.PhoneNumber),
new Claim(ClaimTypes.Email, user.Email),
new Claim(ClaimTypes.Role, user.Role!)
new Claim("Id", user.Id.ToString()),
new Claim("Name", user.Name),
new Claim("Phone", user.PhoneNumber),
new Claim("Email", user.Email),
new Claim("Role", user.Role!)
};


Expand All @@ -81,10 +90,17 @@ public string GenerateToken(Admin user)
expires: DateTime.UtcNow.AddMinutes(expirePeriod),
signingCredentials: credentials);

return new JwtSecurityTokenHandler().WriteToken(token);
var response = new TokenModel()
{
Token = new JwtSecurityTokenHandler().WriteToken(token),
isSuccess = true,
Message = "Token generated successfully!"
};

return response;
}

public string GenerateToken(Mentor user)
public TokenModel GenerateToken(Mentor user)
{
SymmetricSecurityKey securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWTSettings:Secret"]!));
SigningCredentials credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
Expand All @@ -94,11 +110,11 @@ public string 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("UserId", user.Id.ToString()),
new Claim(ClaimTypes.Name, user.Name),
new Claim(ClaimTypes.MobilePhone, user.PhoneNumber),
new Claim(ClaimTypes.Email, user.Email),
new Claim(ClaimTypes.Role, user.Role!)
new Claim("Id", user.Id.ToString()),
new Claim("Name", user.Name),
new Claim("Phone", user.PhoneNumber),
new Claim("Email", user.Email),
new Claim("Role", user.Role!)
};


Expand All @@ -110,7 +126,14 @@ public string GenerateToken(Mentor user)
expires: DateTime.UtcNow.AddMinutes(expirePeriod),
signingCredentials: credentials);

return new JwtSecurityTokenHandler().WriteToken(token);
var response = new TokenModel()
{
Token = new JwtSecurityTokenHandler().WriteToken(token),
isSuccess = true,
Message = "Token generated successfully!"
};

return response;
}
}
}
12 changes: 4 additions & 8 deletions UrphaCapital.Application/AuthServices/IAuthService.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UrphaCapital.Application.ViewModels;
using UrphaCapital.Domain.Entities.Auth;

namespace UrphaCapital.Application.AuthServices
{
public interface IAuthService
{
public string GenerateToken(Student user);
public string GenerateToken(Admin user);
public string GenerateToken(Mentor user);
public TokenModel GenerateToken(Student user);
public TokenModel GenerateToken(Admin user);
public TokenModel GenerateToken(Mentor user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ namespace UrphaCapital.Application.UseCases.Courses.Commands
{
public class DeleteCourseCommand : IRequest<ResponseModel>
{
public long Id { get; set; }
public string Id { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace UrphaCapital.Application.UseCases.Courses.Commands
{
public class UpdateCourseCommand : IRequest<ResponseModel>
{
public long Id { get; set; }
public string Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string Subtitle { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public DeleteCourseCommandHandler(IApplicationDbContext context, IWebHostEnviron

public async Task<ResponseModel> Handle(DeleteCourseCommand request, CancellationToken cancellationToken)
{
var course = await _context.Courses.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
var course = await _context.Courses.FirstOrDefaultAsync(x => x.Id.ToString() == request.Id, cancellationToken);

if (course == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public UpdateCourseCommandHandler(IApplicationDbContext context, IWebHostEnviron

public async Task<ResponseModel> Handle(UpdateCourseCommand request, CancellationToken cancellationToken)
{
var course = await _context.Courses.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
var course = await _context.Courses.FirstOrDefaultAsync(x => x.Id.ToString() == request.Id, cancellationToken);

if (course == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public GetCourseByIdQueryHandler(IApplicationDbContext context)

public async Task<Course> Handle(GetCourseByIdQuery request, CancellationToken cancellationToken)
{
return await _context.Courses.FirstOrDefaultAsync(x => x.Id == request.Id) ??
return await _context.Courses.FirstOrDefaultAsync(x => x.Id.ToString() == request.Id) ??
throw new Exception("Not found");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ namespace UrphaCapital.Application.UseCases.Courses.Queries
{
public class GetCourseByIdQuery : IRequest<Course>
{
public long Id { get; set; }
public string Id { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ public class CreateHomeworkCommand: IRequest<ResponseModel>
public IFormFile FILE { get; set; }
public string Description { get; set; }
public long studentId { get; set; }
public long LessonId { get; set; }
public Guid LessonId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public class UpdateHomeworkCommand: IRequest<ResponseModel>
public IFormFile FILE { get; set; }
public string Description { get; set; }
public long studentId { get; set; }
public long LessonId { get; set; }
public Guid LessonId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace UrphaCapital.Application.UseCases.Lessons.Commands
public class CreateLessonCommand : IRequest<ResponseModel>
{
public string Name { get; set; }
public long CourseId { get; set; }
public Guid CourseId { get; set; }
public IFormFile Video { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ namespace UrphaCapital.Application.UseCases.Lessons.Commands
{
public class DeleteLessonCommand : IRequest<ResponseModel>
{
public long Id { get; set; }
public string Id { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace UrphaCapital.Application.UseCases.Lessons.Commands
{
public class UpdateLessonCommand : IRequest<ResponseModel>
{
public long Id { get; set; }
public string Id { get; set; }
public string Name { get; set; }
public long CourseId { get; set; }
public Guid CourseId { get; set; }
public IFormFile Video { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public DeleteLessonCommandHandler(IApplicationDbContext context, IWebHostEnviron
public async Task<ResponseModel> Handle(DeleteLessonCommand request, CancellationToken cancellationToken)
{

var lesson = await _context.Lessons.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
var lesson = await _context.Lessons.FirstOrDefaultAsync(x => x.Id.ToString() == request.Id, cancellationToken);

if (lesson == null)
return new ResponseModel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public UpdateLessonCommandHandler(IApplicationDbContext context, IWebHostEnviron

public async Task<ResponseModel> Handle(UpdateLessonCommand request, CancellationToken cancellationToken)
{
var lesson = await _context.Lessons.FirstOrDefaultAsync(x => x.Id == request.Id, cancellationToken);
var lesson = await _context.Lessons.FirstOrDefaultAsync(x => x.Id.ToString() == request.Id, cancellationToken);

if (lesson == null)
return new ResponseModel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<IEnumerable<Lesson>> Handle(GetAllLessonsByCourseIdQuery reque
_memoryCache.Set(
key: "lesson",
value: await _context.Lessons
.Where(l => l.CourseId == request.CourseId)
.Where(l => l.CourseId.ToString() == request.CourseId)
.Skip(request.Index - 1)
.Take(request.Count)
.Select(x => new Lesson
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public GetLessonByIdQueryHandler(IApplicationDbContext context)

public async Task<Lesson> Handle(GetLessonByIdQuery request, CancellationToken cancellationToken)
{
return await _context.Lessons.FirstOrDefaultAsync(x => x.Id == request.Id) ??
return await _context.Lessons.FirstOrDefaultAsync(x => x.Id.ToString() == request.Id) ??
throw new NotImplementedException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public GetLessonVideoQueryHandler(IApplicationDbContext context)

public async Task<Stream?> Handle(GetLessonVideoQuery request, CancellationToken cancellationToken)
{
var lesson = await _context.Lessons.FirstOrDefaultAsync(x => x.Id == request.Id);
var lesson = await _context.Lessons.FirstOrDefaultAsync(x => x.Id.ToString() == request.Id);
if (lesson == null)
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace UrphaCapital.Application.UseCases.Lessons.Queries
{
public class GetAllLessonsByCourseIdQuery : IRequest<IEnumerable<Lesson>>
{
public long CourseId { get; set; }
public string CourseId { get; set; }
public int Index { get; set; }
public int Count { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ namespace UrphaCapital.Application.UseCases.Lessons.Queries
{
public class GetLessonByIdQuery : IRequest<Lesson>
{
public long Id { get; set; }
public string Id { get; set; }
}
}
Loading

0 comments on commit 8ccc993

Please sign in to comment.