-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathStudentsController.cs
More file actions
57 lines (52 loc) · 2.28 KB
/
StudentsController.cs
File metadata and controls
57 lines (52 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// -----------------------------------------------------------------------
// Copyright (c) Signature Chess Club & MumsWhoCode. All rights reserved.
// -----------------------------------------------------------------------
using Microsoft.AspNetCore.Mvc;
using RESTFulSense.Controllers;
using SCMS.Services.Api.Models.Foundations.Students;
using SCMS.Services.Api.Models.Foundations.Students.Exceptions;
using SCMS.Services.Api.Services.Foundations.Students;
using System.Threading.Tasks;
namespace SCMS.Services.Api.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class StudentsController : RESTFulController
{
private readonly IStudentService studentService;
public StudentsController(IStudentService studentService) =>
this.studentService = studentService;
[HttpPost]
public async ValueTask<ActionResult<Student>> PostStudentAsync(Student student)
{
try
{
Student createdStudent =
await this.studentService.AddStudentAsync(student);
return Created(createdStudent);
}
catch (StudentValidationException studentValidationException)
{
return BadRequest(studentValidationException.InnerException);
}
catch (StudentDependencyException studentDependencyException)
{
return InternalServerError(studentDependencyException);
}
catch (StudentDependencyValidationException studentDependencyValidationException)
when (studentDependencyValidationException.InnerException is AlreadyExistsStudentException)
{
return Conflict(studentDependencyValidationException.InnerException);
}
catch (StudentDependencyValidationException studentDependencyValidationException)
when (studentDependencyValidationException.InnerException is InvalidStudentReferenceException)
{
return FailedDependency(studentDependencyValidationException.InnerException);
}
catch (StudentServiceException studentServiceException)
{
return InternalServerError(studentServiceException);
}
}
}
}