Answer the question
In order to leave comments, you need to log in
How to design a RESTful API?
There is the following data structure:
Mongo models: User, Course, Lesson
User roles: admin, teacher, student
Relationships will be like this:
User: {
name: 'Teacher',
role: 'teacher',
courses: [{
title: 'Course',
lessons: [{
title: 'Lesson'
}]
}]
}
POST - Create
GET - Read
PUT - Update
DELETE - Delete
GET, POST /users
PUT, GET, DELETE /users/:uuid
POST users/:userUuid/courses - req.body { title } // создаем курс у пользователя
userUuid === req.user.uuid // проверяем права доступа
course = Course.create({ title })
user = User.find({ uuid: userUuid })
user.courses.push(course._id).save()
GET /courses/:courseUuid
course = Course.findOne({ uuid: courseUuid })
user = User.findOne({ 'courses': course._id})
user.uuid === req.user.uuid // проверяем права доступа, если true возвращаем курс
POST user/:userUuid/course
но
GET /course/:courseUuid
POST /courses/:courseUuid/lessons
course = Course.findOne({ uuid: courseUuid })
user = User.findOne({ courses: course._id})
user.uuid === req.user.uuid // проверяем права доступа, если true - можно создавать урок
lesson = Lesson.create({ title })
course.lessons.push(lesson._id)
GET /lesson/:lessonUuid
lesson = Lesson.findOne({ uuid: lessonUuid })
course = Course.findOne({ lessons: lesson._id })
user = User.findOne({ courses: course._id})
user.uuid === req.user.uuid // проверяем права доступа, если true - можно вернуть урок
Answer the question
In order to leave comments, you need to log in
Somehow everything is not optimal and it doesn’t look like RESTful, please advise how it is usually organized in general?
Suggestion: RESTful looks good only in tightly synthetic examples of an abstract object catalog without business logic and relationships.but it's some weird restful,
POST user/:userUuid/course
Why notPOST /course/
? Your course is an independent object, it is not a property of the user.
user data is passed either as a parameter in the link, or as a request header , so you can
GET
, POST /course/
:
courseUuid /:lessonUuid common
approach to have a unified api, with the same POST and GET (and additional low hierarchy requests like /lesson/:lessonUuid)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question