diff --git a/src/main/java/com/goorm/jejuPowerTest/app/base/db/IntiData.java b/src/main/java/com/goorm/jejuPowerTest/app/base/db/IntiData.java index 0b5e4b6..6ca29b9 100644 --- a/src/main/java/com/goorm/jejuPowerTest/app/base/db/IntiData.java +++ b/src/main/java/com/goorm/jejuPowerTest/app/base/db/IntiData.java @@ -6,16 +6,18 @@ import com.goorm.jejuPowerTest.app.content.service.ContentService; import com.goorm.jejuPowerTest.app.place.dto.CreatePlaceDTO; import com.goorm.jejuPowerTest.app.place.service.PlaceService; +import com.goorm.jejuPowerTest.app.view.entity.View; +import com.goorm.jejuPowerTest.app.view.service.ViewService; import org.springframework.boot.CommandLineRunner; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class IntiData { - private boolean initDataDone = true; + private boolean initDataDone = false; @Bean CommandLineRunner init( - ContentService contentService, PlaceService placeService, AvatarService avatarService + ContentService contentService, PlaceService placeService, AvatarService avatarService, ViewService viewService ) { return args -> { if(initDataDone){ @@ -72,6 +74,12 @@ CommandLineRunner init( avatarService.create(createAvatarDTO8); avatarService.create(createAvatarDTO9); avatarService.create(createAvatarDTO10); + + // view + View view = View.builder() + .count(0L) + .build(); + viewService.create(view); }; } } diff --git a/src/main/java/com/goorm/jejuPowerTest/app/result/controller/ResultController.java b/src/main/java/com/goorm/jejuPowerTest/app/result/controller/ResultController.java index 3e170a6..54c121f 100644 --- a/src/main/java/com/goorm/jejuPowerTest/app/result/controller/ResultController.java +++ b/src/main/java/com/goorm/jejuPowerTest/app/result/controller/ResultController.java @@ -10,6 +10,7 @@ import com.goorm.jejuPowerTest.app.content.service.ContentService; import com.goorm.jejuPowerTest.app.result.dto.ResponseResultDTO; import com.goorm.jejuPowerTest.app.result.service.ResultService; +import com.goorm.jejuPowerTest.app.view.service.ViewService; import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -24,12 +25,15 @@ public class ResultController { private final ResultService resultService; + private final ViewService viewService; + @PostMapping("/result") public ResponseEntity getAnswer(@RequestBody RequestDTO[] requestDTO){ // List contentDTOS = contentService.getAnswers(requestDTO); // ResponseAvatarDTO avatarDTO; // ResponsePlaceDTO placeDTO = placeService.getAnswer(requestDTO); - + ResponseResultDTO responseResultDTO = resultService.getAnswers(requestDTO); + viewService.increaseView(); return new ResponseEntity<>(resultService.getAnswers(requestDTO), HttpStatus.OK); } } diff --git a/src/main/java/com/goorm/jejuPowerTest/app/view/controller/ViewController.java b/src/main/java/com/goorm/jejuPowerTest/app/view/controller/ViewController.java new file mode 100644 index 0000000..d3d8b46 --- /dev/null +++ b/src/main/java/com/goorm/jejuPowerTest/app/view/controller/ViewController.java @@ -0,0 +1,23 @@ +package com.goorm.jejuPowerTest.app.view.controller; + +import com.goorm.jejuPowerTest.app.view.dto.ResponseViewDTO; +import com.goorm.jejuPowerTest.app.view.service.ViewService; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +@Controller +@RequiredArgsConstructor +public class ViewController { + + private final ViewService viewService; + + @PostMapping("/view") + public ResponseEntity getView(){ + + return new ResponseEntity<>(viewService.getView(), HttpStatus.OK); + } +} diff --git a/src/main/java/com/goorm/jejuPowerTest/app/view/dto/ResponseViewDTO.java b/src/main/java/com/goorm/jejuPowerTest/app/view/dto/ResponseViewDTO.java new file mode 100644 index 0000000..e6df1df --- /dev/null +++ b/src/main/java/com/goorm/jejuPowerTest/app/view/dto/ResponseViewDTO.java @@ -0,0 +1,12 @@ +package com.goorm.jejuPowerTest.app.view.dto; + +import lombok.*; + +@Builder +@Getter +@NoArgsConstructor +@AllArgsConstructor +@ToString +public class ResponseViewDTO { + Long view; +} diff --git a/src/main/java/com/goorm/jejuPowerTest/app/view/entity/View.java b/src/main/java/com/goorm/jejuPowerTest/app/view/entity/View.java new file mode 100644 index 0000000..5265152 --- /dev/null +++ b/src/main/java/com/goorm/jejuPowerTest/app/view/entity/View.java @@ -0,0 +1,22 @@ +package com.goorm.jejuPowerTest.app.view.entity; + +import com.goorm.jejuPowerTest.app.base.entity.BaseEntity; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import lombok.experimental.SuperBuilder; + +import javax.persistence.Column; +import javax.persistence.Entity; + +import static lombok.AccessLevel.PROTECTED; + +@Entity +@Getter +@Setter +@SuperBuilder +@NoArgsConstructor(access = PROTECTED) +public class View extends BaseEntity { + @Column() + Long count; +} diff --git a/src/main/java/com/goorm/jejuPowerTest/app/view/repository/ViewRepository.java b/src/main/java/com/goorm/jejuPowerTest/app/view/repository/ViewRepository.java new file mode 100644 index 0000000..1cd8c1a --- /dev/null +++ b/src/main/java/com/goorm/jejuPowerTest/app/view/repository/ViewRepository.java @@ -0,0 +1,13 @@ +package com.goorm.jejuPowerTest.app.view.repository; + +import com.goorm.jejuPowerTest.app.place.entity.Place; +import com.goorm.jejuPowerTest.app.view.entity.View; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +public interface ViewRepository extends JpaRepository { + @Query("update View v set v.count = v.count + 1 where v.id = :id") + int updateView(@Param("id") Long id); +} diff --git a/src/main/java/com/goorm/jejuPowerTest/app/view/service/ViewService.java b/src/main/java/com/goorm/jejuPowerTest/app/view/service/ViewService.java new file mode 100644 index 0000000..3bb5f64 --- /dev/null +++ b/src/main/java/com/goorm/jejuPowerTest/app/view/service/ViewService.java @@ -0,0 +1,34 @@ +package com.goorm.jejuPowerTest.app.view.service; + +import com.goorm.jejuPowerTest.app.view.dto.ResponseViewDTO; +import com.goorm.jejuPowerTest.app.view.entity.View; +import com.goorm.jejuPowerTest.app.view.repository.ViewRepository; +import com.goorm.jejuPowerTest.global.mapper.ViewMapper; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class ViewService { + + private final ViewRepository viewRepository; + + private final ViewMapper viewMapper; + + @Transactional + public void create(View view){ + viewRepository.save(view); + } + + public ResponseViewDTO getView(){ + View view = viewRepository.findById(1L).get(); + return viewMapper.entityToDto(view); + } + + @Transactional + public void increaseView(){ + viewRepository.updateView(1L); + } +} diff --git a/src/main/java/com/goorm/jejuPowerTest/global/mapper/ViewMapper.java b/src/main/java/com/goorm/jejuPowerTest/global/mapper/ViewMapper.java new file mode 100644 index 0000000..a90ded4 --- /dev/null +++ b/src/main/java/com/goorm/jejuPowerTest/global/mapper/ViewMapper.java @@ -0,0 +1,16 @@ +package com.goorm.jejuPowerTest.global.mapper; + +import com.goorm.jejuPowerTest.app.view.dto.ResponseViewDTO; +import com.goorm.jejuPowerTest.app.view.entity.View; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class ViewMapper { + public ResponseViewDTO entityToDto(View view){ + return ResponseViewDTO.builder() + .view(view.getCount()) + .build(); + } +}