Skip to content
Snippets Groups Projects
Commit 4488aabf authored by esantangelo's avatar esantangelo
Browse files

add endpoint Matriculate

parent b2166670
No related branches found
No related tags found
Loading
...@@ -57,5 +57,21 @@ namespace Tsi1.Api.Controllers ...@@ -57,5 +57,21 @@ namespace Tsi1.Api.Controllers
return Ok(); return Ok();
} }
[Authorize(Roles = UserTypes.Student)]
[HttpPost("Matriculate/{courseId}")]
public async Task<IActionResult> Matriculate(int courseId)
{
var userId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "Id").Value);
var result = await _courseService.Matriculate(userId, courseId);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok();
}
} }
} }
...@@ -13,5 +13,7 @@ namespace Tsi1.BusinessLayer.Interfaces ...@@ -13,5 +13,7 @@ namespace Tsi1.BusinessLayer.Interfaces
Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(string userType, int tenantId); Task<ServiceResult<List<CoursePreviewDto>>> GetCoursePreviews(string userType, int tenantId);
Task<ServiceResult<Course>> Create(CourseCreateDto newCourse); Task<ServiceResult<Course>> Create(CourseCreateDto newCourse);
Task<ServiceResult<bool>> Matriculate(int userId, int courseId);
} }
} }
using AutoMapper; using AutoMapper;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.FileProviders;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
...@@ -32,8 +33,17 @@ namespace Tsi1.BusinessLayer.Services ...@@ -32,8 +33,17 @@ namespace Tsi1.BusinessLayer.Services
var course = _mapper.Map<Course>(newCourse); var course = _mapper.Map<Course>(newCourse);
_context.Courses.Add(course); _context.Courses.Add(course);
await _context.SaveChangesAsync(); try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.DuplicateCourseName, newCourse.Name);
return result;
}
result.Data = course; result.Data = course;
return result; return result;
} }
...@@ -65,5 +75,42 @@ namespace Tsi1.BusinessLayer.Services ...@@ -65,5 +75,42 @@ namespace Tsi1.BusinessLayer.Services
return result; return result;
} }
public async Task<ServiceResult<bool>> Matriculate(int userId, int courseId)
{
var result = new ServiceResult<bool>();
var user = await _context.Users
.Include(x => x.Student)
.FirstOrDefaultAsync(x => x.Id == userId);
if (user == null || user.Student == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
return result;
}
var course = await _context.Courses.FirstOrDefaultAsync(x => x.Id == courseId);
if (course == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.CourseDoesNotExist, courseId);
return result;
}
var studentCourse = new StudentCourse
{
CourseId = courseId,
StudentId = user.Student.Id
};
_context.StudentCourses.Add(studentCourse);
await _context.SaveChangesAsync();
return result;
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment