-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from likelionknu/agape1225_test_junit
Agape1225 test junit
- Loading branch information
Showing
15 changed files
with
445 additions
and
6 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
19 changes: 19 additions & 0 deletions
19
src/main/java/com/springboot/applypage/config/LocalDateTimeSerializer.java
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,19 @@ | ||
package com.springboot.applypage.config; | ||
|
||
import com.fasterxml.jackson.databind.JsonSerializable; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonPrimitive; | ||
import com.google.gson.JsonSerializationContext; | ||
import com.google.gson.JsonSerializer; | ||
|
||
import java.lang.reflect.Type; | ||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class LocalDateTimeSerializer implements JsonSerializer<LocalDate> { | ||
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); | ||
@Override | ||
public JsonElement serialize(LocalDate localDate, Type type, JsonSerializationContext jsonSerializationContext) { | ||
return new JsonPrimitive(formatter.format(localDate)); | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
src/main/java/com/springboot/applypage/controller/UserController.java
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,37 @@ | ||
package com.springboot.applypage.controller; | ||
|
||
import com.springboot.applypage.data.dto.UserDto; | ||
import com.springboot.applypage.service.UserService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/user") | ||
public class UserController { | ||
private final UserService userService; | ||
@Autowired | ||
public UserController(UserService userService){ | ||
this.userService = userService; | ||
} | ||
|
||
@GetMapping() | ||
public ResponseEntity<UserDto> getUser(Long sid){ | ||
UserDto userDto = userService.getUser(sid); | ||
System.out.println(userDto); | ||
return ResponseEntity.status(HttpStatus.OK).body(userDto); | ||
} | ||
|
||
@PostMapping() | ||
public ResponseEntity<UserDto> createUser(@RequestBody UserDto userDto){ | ||
UserDto savedUser = userService.saveUser(userDto); | ||
return ResponseEntity.status(HttpStatus.OK).body(savedUser); | ||
} | ||
|
||
@DeleteMapping() | ||
public ResponseEntity<String> deleteUser(Long sid) throws Exception{ | ||
userService.deleteUser(sid); | ||
return ResponseEntity.status(HttpStatus.OK).body("정상적으로 삭제되었습니다."); | ||
} | ||
} |
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,9 @@ | ||
package com.springboot.applypage.data.dao; | ||
|
||
import com.springboot.applypage.data.entity.User; | ||
|
||
public interface UserDAO { | ||
User insertUser(User user); | ||
User selectUser(Long sid); | ||
void deleteUser(Long sid) throws Exception; | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/com/springboot/applypage/data/dao/impl/UserDAOImpl.java
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 com.springboot.applypage.data.dao.impl; | ||
|
||
import com.springboot.applypage.data.dao.UserDAO; | ||
import com.springboot.applypage.data.entity.User; | ||
import com.springboot.applypage.data.repository.UserRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Optional; | ||
|
||
@Component | ||
public class UserDAOImpl implements UserDAO { | ||
|
||
private final UserRepository userRepository; | ||
@Autowired | ||
public UserDAOImpl(UserRepository userRepository){ | ||
this.userRepository = userRepository; | ||
} | ||
|
||
@Override | ||
public User insertUser(User user) { | ||
|
||
User savedUser = userRepository.save(user); | ||
|
||
return savedUser; | ||
} | ||
|
||
@Override | ||
public User selectUser(Long sid) { | ||
User selectedUser = userRepository.getById(sid); | ||
return selectedUser; | ||
} | ||
|
||
@Override | ||
public void deleteUser(Long sid) throws Exception { | ||
Optional<User> selectedUser = userRepository.findById(sid); | ||
|
||
if(selectedUser.isPresent()){ | ||
User user = selectedUser.get(); | ||
userRepository.delete(user); | ||
}else{ | ||
throw new Exception(); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/springboot/applypage/data/dto/UserDto.java
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,27 @@ | ||
package com.springboot.applypage.data.dto; | ||
|
||
import com.springboot.applypage.data.enumdata.Role; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.LocalDate; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Builder | ||
public class UserDto { | ||
|
||
private Long sid; | ||
|
||
private String email; | ||
|
||
private String name; | ||
|
||
private LocalDate birthDay; | ||
|
||
private Role role; | ||
|
||
} |
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
37 changes: 37 additions & 0 deletions
37
src/main/java/com/springboot/applypage/data/entity/User.java
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,37 @@ | ||
package com.springboot.applypage.data.entity; | ||
|
||
import com.springboot.applypage.data.enumdata.Role; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.hibernate.annotations.ColumnDefault; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.Id; | ||
import javax.persistence.Table; | ||
import java.time.LocalDate; | ||
|
||
@Entity | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Table(name="user") | ||
public class User extends BaseEntity{ | ||
|
||
@Id | ||
private Long sid; | ||
|
||
@Column(nullable = false) | ||
private String email; | ||
|
||
@Column(nullable = false) | ||
private String name; | ||
|
||
@Column(nullable = false) | ||
private LocalDate birthDay; | ||
|
||
@Column(nullable = false) | ||
@ColumnDefault("BABY") | ||
private Role role; | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/com/springboot/applypage/data/enumdata/Role.java
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,5 @@ | ||
package com.springboot.applypage.data.enumdata; | ||
|
||
public enum Role { | ||
ROOT, REPR, MANA, BABY | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/springboot/applypage/data/repository/UserRepository.java
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,7 @@ | ||
package com.springboot.applypage.data.repository; | ||
|
||
import com.springboot.applypage.data.entity.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/springboot/applypage/service/UserService.java
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,9 @@ | ||
package com.springboot.applypage.service; | ||
|
||
import com.springboot.applypage.data.dto.UserDto; | ||
|
||
public interface UserService { | ||
UserDto getUser(Long sid); | ||
UserDto saveUser(UserDto user); | ||
void deleteUser(Long sid) throws Exception; | ||
} |
62 changes: 62 additions & 0 deletions
62
src/main/java/com/springboot/applypage/service/impl/UserServiceImpl.java
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,62 @@ | ||
package com.springboot.applypage.service.impl; | ||
|
||
import com.springboot.applypage.data.dao.UserDAO; | ||
import com.springboot.applypage.data.dto.UserDto; | ||
import com.springboot.applypage.data.entity.User; | ||
import com.springboot.applypage.service.UserService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class UserServiceImpl implements UserService { | ||
|
||
private final UserDAO userDAO; | ||
|
||
@Autowired | ||
public UserServiceImpl(UserDAO userDAO){ | ||
this.userDAO = userDAO; | ||
} | ||
|
||
@Override | ||
public UserDto getUser(Long sid) { | ||
User selectedUser = userDAO.selectUser(sid); | ||
|
||
UserDto responseUser = new UserDto(); | ||
|
||
responseUser.setName(selectedUser.getName()); | ||
responseUser.setRole(selectedUser.getRole()); | ||
responseUser.setEmail(selectedUser.getEmail()); | ||
responseUser.setSid(selectedUser.getSid()); | ||
responseUser.setBirthDay(selectedUser.getBirthDay()); | ||
|
||
|
||
return responseUser; | ||
} | ||
|
||
@Override | ||
public UserDto saveUser(UserDto user) { | ||
//User savedUser = userDAO.insertUser(user); | ||
User savedUser = new User(); | ||
savedUser.setEmail(user.getEmail()); | ||
savedUser.setBirthDay(user.getBirthDay()); | ||
savedUser.setName(user.getName()); | ||
savedUser.setRole(user.getRole()); | ||
savedUser.setSid(user.getSid()); | ||
|
||
savedUser = userDAO.insertUser(savedUser); | ||
|
||
UserDto responseUser = new UserDto(); | ||
responseUser.setRole(savedUser.getRole()); | ||
responseUser.setEmail(savedUser.getEmail()); | ||
responseUser.setSid(savedUser.getSid()); | ||
responseUser.setBirthDay(savedUser.getBirthDay()); | ||
responseUser.setName(savedUser.getName()); | ||
|
||
return responseUser; | ||
} | ||
|
||
@Override | ||
public void deleteUser(Long sid) throws Exception{ | ||
userDAO.deleteUser(sid); | ||
} | ||
} |
Oops, something went wrong.