Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lesson9 #12

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Lesson9 #12

wants to merge 14 commits into from

Conversation

mk17ru
Copy link
Owner

@mk17ru mk17ru commented Apr 12, 2021

No description provided.

@PostMapping("/{city}")
@ResponseBody
fun addCity(@PathVariable city: String): String {
cityRepository.save(City(0, city))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут всё таки должна возвращаться только что созданная сущность

@Tag(name = "user", description = "The User API")
@Controller
@RequestMapping("/api/user")
class UserController(private val userRepository: UserService, private val cityRepository: CityRepository) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rest предполагает, что у тебя есть описание какого то ресурса с примерно такими операциями:

  • GET /api/users - получить всех пользователей
  • GET /api/users/{id} - получить одного пользователя
  • POST /api/users - создать пользователя
  • PUT /api/users/{id} - изменить пользователя
  • DELETE /api/users/{id} - удалить пользователя



@Operation(summary = "Add user", tags = ["user"])
@PostMapping("/save/{login}/{firstname}/{lastname}/{city}")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это делается через body, вот так принимать параметры максимально небезопасно, так как многие балансировщики логируют путь

@PathVariable city: String): String {
val c = cityRepository.findCityByName(city) ?: throw ResponseStatusException(NOT_FOUND, "City don't exist")
userRepository.addUser(User(0, login, firstname, lastname, c.id))
return "UserSaved"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Таки надо возвращать пользователя



@Operation(summary = "Update user", tags = ["user"])
@PutMapping("/update/{id}/{login}")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Поля обычно принимаются через body

import javax.persistence.*

@Entity
class User(@Id @GeneratedValue var id: Long? = null,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А для чего все значения заполнены по умолчанию?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants