-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
3. Rest API. Базовая структура. [#505107]
- Loading branch information
1 parent
bc37e06
commit 4b43824
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package ru.job4j.api.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import ru.job4j.api.entity.UserPost; | ||
import ru.job4j.api.enums.Statuses; | ||
import ru.job4j.api.repository.UserPostRepository; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/post") | ||
public class PostController { | ||
|
||
private final UserPostRepository userPostRepository; | ||
|
||
@GetMapping("/{postId}") | ||
public ResponseEntity<UserPost> get(@PathVariable Long postId) { | ||
return userPostRepository.findById(postId) | ||
.map(ResponseEntity::ok) | ||
.orElseGet(() -> ResponseEntity.notFound().build()); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<UserPost> save(@RequestBody UserPost post) { | ||
post.setStatus(Statuses.A); | ||
return ResponseEntity.ok(userPostRepository.save(post)); | ||
} | ||
|
||
@PutMapping | ||
@ResponseStatus(HttpStatus.OK) | ||
public void update(@RequestBody UserPost post) { | ||
userPostRepository.save(post); | ||
} | ||
|
||
@DeleteMapping("/{postId}") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void delete(@PathVariable Long postId) { | ||
userPostRepository.findById(postId).ifPresent(post -> { | ||
post.setStatus(Statuses.D); | ||
userPostRepository.save(post); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package ru.job4j.api.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import ru.job4j.api.entity.User; | ||
import ru.job4j.api.enums.Statuses; | ||
import ru.job4j.api.repository.UserRepository; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/user") | ||
public class UserController { | ||
|
||
private final UserRepository userRepository; | ||
|
||
@GetMapping("/{userId}") | ||
public ResponseEntity<User> get(@PathVariable Long userId) { | ||
return userRepository.findById(userId) | ||
.map(ResponseEntity::ok) | ||
.orElseGet(() -> ResponseEntity.notFound().build()); | ||
} | ||
|
||
@PostMapping | ||
public ResponseEntity<User> save(@RequestBody User user) { | ||
user.setStatus(Statuses.A); | ||
return ResponseEntity.ok(userRepository.save(user)); | ||
} | ||
|
||
@PutMapping | ||
@ResponseStatus(HttpStatus.OK) | ||
public void update(@RequestBody User user) { | ||
userRepository.save(user); | ||
} | ||
|
||
@DeleteMapping("/{userId}") | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void delete(@PathVariable Long userId) { | ||
userRepository.findById(userId).ifPresent(user -> { | ||
user.setStatus(Statuses.D); | ||
userRepository.save(user); | ||
}); | ||
} | ||
} |