I'm new to Asp.net MVC and to Web APIs, just learning it.
lets say that we have
Models:
public class Student
{
public int StudentID { get; set; }
public string StudentName { get; set; }
}
public class Course
{
public int CourseID { get; set; }
public string CourseName { get; set; }
public IEnumerable<int> StudentIds { get; set; }
}
controllers:
- User Controller - implements Post, Put, Get, Delete
- Course Controller - implements Post, Put, Get, Delete
each controller uses some kind of repository to get/add/remove/update data
lets say:
courses Repository is static Dictionary<'int, Course> Courses
students Repository is *static Dictionary<'int, Student> Students *
We do not use Entity Framework!
Custom Routing
Post /api/courses/{courseid}/users/{userid}
this should go to Course Controller and add the user id to the list
[Route("api/courses/{CourseID}/students/{StudentID}")]
[HttpPost]
public HttpResponseMessage AddStudentToCourse(int CourseID, int StudentID)
{
// adding student id to the course
...
}
Questions:
- should i use custom route to add the mapping between courses and users
- should i check if the student exists ? or this is kind of an agreement with the client that the input is valid
- if we do check - who should do it ? the controller ? the model ? some kind of data access layer?
- if its the CourseController / CourseModel that means that it should "know" the two repositories (the courses repository and the students repository) right ?