Skip to content

Commit

Permalink
Merge pull request #8 from likelionknu/agape1225_test_junit
Browse files Browse the repository at this point in the history
Agape1225 test junit
  • Loading branch information
agape1225 authored Mar 11, 2023
2 parents a7a6ea0 + 3a39528 commit eed72c4
Show file tree
Hide file tree
Showing 15 changed files with 445 additions and 6 deletions.
5 changes: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
<optional>true</optional>
</dependency>
<dependency>

<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
Expand All @@ -40,6 +39,10 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
Expand Down
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

//@Configuration
//@EnableSwagger2
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

@Bean
Expand Down
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("정상적으로 삭제되었습니다.");
}
}
9 changes: 9 additions & 0 deletions src/main/java/com/springboot/applypage/data/dao/UserDAO.java
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;
}
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 src/main/java/com/springboot/applypage/data/dto/UserDto.java
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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import lombok.Setter;
import lombok.ToString;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;
import java.time.LocalDateTime;
Expand All @@ -21,7 +19,7 @@
public class BaseEntity {

@CreatedDate
@Column(updatable = false)
//@Column(updatable = false)
private LocalDateTime createdAt;

@LastModifiedDate
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/springboot/applypage/data/entity/User.java
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;
}
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
}
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> {
}
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;
}
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);
}
}
Loading

0 comments on commit eed72c4

Please sign in to comment.