Skip to content

Commit

Permalink
feat : view API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
doolysmile committed Nov 17, 2022
1 parent 54a1ff1 commit 354a32f
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/main/java/com/goorm/jejuPowerTest/app/base/db/IntiData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,12 +25,15 @@
public class ResultController {
private final ResultService resultService;

private final ViewService viewService;

@PostMapping("/result")
public ResponseEntity<ResponseResultDTO> getAnswer(@RequestBody RequestDTO[] requestDTO){
// List<ResponseContentDTO> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<ResponseViewDTO> getView(){

return new ResponseEntity<>(viewService.getView(), HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.goorm.jejuPowerTest.app.view.dto;

import lombok.*;

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class ResponseViewDTO {
Long view;
}
22 changes: 22 additions & 0 deletions src/main/java/com/goorm/jejuPowerTest/app/view/entity/View.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<View, Long> {
@Query("update View v set v.count = v.count + 1 where v.id = :id")
int updateView(@Param("id") Long id);
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 354a32f

Please sign in to comment.