-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserController.kt
81 lines (59 loc) · 2.06 KB
/
UserController.kt
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package de.jeske.restapiwithopenai.controller
import de.jeske.restapiwithopenai.dtos.UserDTO
import de.jeske.restapiwithopenai.dtos.UserIdDTO
import de.jeske.restapiwithopenai.entities.UserEntity
import de.jeske.restapiwithopenai.modells.User
import de.jeske.restapiwithopenai.services.UserService
import org.bson.types.ObjectId
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
@RestController
@RequestMapping("/user")
class UserController {
@Autowired
private lateinit var userService: UserService
@GetMapping
fun getUser(@RequestParam id: String) : UserDTO {
val objectId = ObjectId(id)
val user = userService.handleGetUserById(objectId)
return UserDTO(user)
}
@PostMapping
fun createUser(
@RequestParam email: String,
@RequestParam surname: String,
@RequestParam firstname: String
): UserIdDTO? {
val user = User(
email = email,
surname = surname,
firstname = firstname
)
val acknowledged = userService.handleCreateUser(user)
return UserIdDTO(user)
}
@PutMapping
fun updateUser(
@RequestParam id: String,
@RequestParam email: String,
@RequestParam surname: String,
@RequestParam firstname: String,
) : UserDTO? {
val userDTO = UserDTO(id, email, surname, firstname)
val user = userService.handleUpdateUser(userDTO)
return UserDTO(user)
}
@DeleteMapping
fun deleteUser(
@RequestParam id: String
) : Boolean? {
val objectId = ObjectId(id)
return userService.handleDeleteUser(objectId)
}
}