Last active
April 22, 2019 02:58
-
-
Save hw0k/752a5b4fb321219717dfba4931804fca to your computer and use it in GitHub Desktop.
190422 Web Programming Summary
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
package kr.hs.dgsw.spring_mini_blog.domain; | |
import com.fasterxml.jackson.annotation.JsonFormat; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import lombok.AllArgsConstructor; | |
import lombok.Data; | |
import lombok.NoArgsConstructor; | |
import org.hibernate.annotations.CreationTimestamp; | |
import org.hibernate.annotations.UpdateTimestamp; | |
import javax.persistence.*; | |
import java.time.LocalDateTime; | |
import java.util.UUID; | |
@Entity | |
@Data | |
@NoArgsConstructor | |
@AllArgsConstructor | |
public class Post { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private Long id; | |
@Column(nullable = false) | |
private String title; | |
@Column(nullable = false) | |
private String content; | |
@Column(nullable = false) | |
private Long userid; | |
@Column | |
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) | |
private UUID picture; | |
@CreationTimestamp | |
@Column(nullable = false, updatable = false) | |
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") | |
private LocalDateTime created; | |
@UpdateTimestamp | |
@Column(nullable = false) | |
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") | |
private LocalDateTime updated; | |
} |
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
package kr.hs.dgsw.spring_mini_blog.controller; | |
import kr.hs.dgsw.spring_mini_blog.domain.Post; | |
import kr.hs.dgsw.spring_mini_blog.service.PostService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.bind.annotation.*; | |
import java.util.List; | |
@RestController | |
public class PostController { | |
private final PostService postService; | |
@Autowired | |
public PostController(PostService postService) { | |
this.postService = postService; | |
} | |
@GetMapping("/post") | |
public List<Post> list() { | |
return postService.list(); | |
} | |
@GetMapping("/post/{id}") | |
public Post get(@PathVariable Long id) { | |
return postService.get(id); | |
} | |
@PostMapping("/post") | |
public Post create(@RequestBody Post post) { | |
return postService.create(post); | |
} | |
@PutMapping("/post/{id}") | |
public Post update(@PathVariable Long id, @RequestBody Post post) { | |
return postService.update(id, post); | |
} | |
@DeleteMapping("/post/{id}") | |
public Post remove(@PathVariable Long id) { | |
return postService.remove(id); | |
} | |
} |
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
package kr.hs.dgsw.spring_mini_blog.repository; | |
import kr.hs.dgsw.spring_mini_blog.domain.Post; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
public interface PostRepository extends JpaRepository<Post, Long> { | |
} |
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
package kr.hs.dgsw.spring_mini_blog.service; | |
import kr.hs.dgsw.spring_mini_blog.domain.Post; | |
import kr.hs.dgsw.spring_mini_blog.repository.PostRepository; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import java.util.List; | |
@Service | |
public class PostServiceImpl implements PostService { | |
private final PostRepository postRepository; | |
@Autowired | |
public PostServiceImpl(PostRepository postRepository) { | |
this.postRepository = postRepository; | |
} | |
@Override | |
public List<Post> list() { | |
return postRepository.findAll(); | |
} | |
@Override | |
public Post get(Long id) { | |
return postRepository.findById(id).orElse(null); | |
} | |
@Override | |
public Post create(Post post) { | |
return postRepository.save(post); | |
} | |
@Override | |
public Post update(Long id, Post post) { | |
if (!postRepository.findById(id).isPresent()) { | |
return null; | |
} | |
post.setId(id); | |
return postRepository.save(post); | |
} | |
@Override | |
public Post remove(Long id) { | |
postRepository.deleteById(id); | |
return null; | |
} | |
} |
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
package kr.hs.dgsw.spring_mini_blog.domain; | |
import com.fasterxml.jackson.annotation.JsonFormat; | |
import com.fasterxml.jackson.annotation.JsonProperty; | |
import lombok.AllArgsConstructor; | |
import lombok.Data; | |
import lombok.NoArgsConstructor; | |
import org.hibernate.annotations.CreationTimestamp; | |
import org.hibernate.annotations.UpdateTimestamp; | |
import javax.persistence.*; | |
import java.time.LocalDateTime; | |
import java.util.UUID; | |
import kr.hs.dgsw.spring_mini_blog.util.EncryptUtil; | |
@Entity | |
@Data | |
@NoArgsConstructor | |
@AllArgsConstructor | |
public class User { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private Long id; | |
@Column(nullable = false, unique = true, length = 31) | |
private String account; | |
@Column(nullable = false) | |
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) | |
private String password; | |
public void setPassword(String password) { | |
this.password = EncryptUtil.encryptString(password); | |
} | |
@Column(nullable = false, length = 31) | |
private String name; | |
@Column(unique = true) | |
private String email; | |
@Column(unique = true) | |
private String phone; | |
@Column | |
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) | |
private UUID profile; | |
@CreationTimestamp | |
@Column(nullable = false, updatable = false) | |
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") | |
private LocalDateTime created; | |
@UpdateTimestamp | |
@Column(nullable = false) | |
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") | |
private LocalDateTime updated; | |
} |
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
package kr.hs.dgsw.spring_mini_blog.controller; | |
import kr.hs.dgsw.spring_mini_blog.domain.User; | |
import kr.hs.dgsw.spring_mini_blog.service.UserService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.web.bind.annotation.*; | |
import java.util.List; | |
@RestController | |
public class UserController { | |
private final UserService userService; | |
@Autowired | |
public UserController(UserService userService) { | |
this.userService = userService; | |
} | |
@GetMapping("/user") | |
public List<User> list() { | |
return userService.list(); | |
} | |
@GetMapping("/user/{id}") | |
public User get(@PathVariable Long id) { | |
return userService.get(id); | |
} | |
@PostMapping("/user") | |
public User create(@RequestBody User user) { | |
return userService.create(user); | |
} | |
@PutMapping("/user/{id}") | |
public User update(@PathVariable Long id, @RequestBody User user) { | |
return userService.update(id, user); | |
} | |
@DeleteMapping("/user/{id}") | |
public User remove(@PathVariable Long id) { | |
return userService.remove(id); | |
} | |
@PostMapping("/user/login") | |
public User login(@RequestBody User user) { | |
return userService.login(user); | |
} | |
} |
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
package kr.hs.dgsw.spring_mini_blog.repository; | |
import kr.hs.dgsw.spring_mini_blog.domain.User; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
import java.util.Optional; | |
public interface UserRepository extends JpaRepository<User, Long> { | |
Optional<User> findByAccountAndPassword(String account, String password); | |
} |
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
package kr.hs.dgsw.spring_mini_blog.service; | |
import kr.hs.dgsw.spring_mini_blog.domain.User; | |
import kr.hs.dgsw.spring_mini_blog.repository.UserRepository; | |
import kr.hs.dgsw.spring_mini_blog.util.EncryptUtil; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import java.util.List; | |
@Service | |
public class UserServiceImpl implements UserService { | |
private final UserRepository userRepository; | |
@Autowired | |
public UserServiceImpl(UserRepository userRepository) { | |
this.userRepository = userRepository; | |
} | |
@Override | |
public List<User> list() { | |
return userRepository.findAll(); | |
} | |
@Override | |
public User get(Long id) { | |
return userRepository.findById(id).orElse(null); | |
} | |
@Override | |
public User create(User user) { | |
return userRepository.save(user); | |
} | |
@Override | |
public User update(Long id, User user) { | |
if (!userRepository.findById(id).isPresent()) { | |
return null; | |
} | |
user.setId(id); | |
return userRepository.save(user); | |
} | |
@Override | |
public User remove(Long id) { | |
userRepository.deleteById(id); | |
return null; | |
} | |
@Override | |
public User login(User user) { | |
return userRepository | |
.findByAccountAndPassword(user.getAccount(), user.getPassword()) | |
.orElse(null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment