Skip to content

Commit

Permalink
feat: register suggestion feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jcw1031 committed May 6, 2024
1 parent 6d14038 commit 71ead9d
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/java/ac/knu/likeknu/controller/SuggestionController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ac.knu.likeknu.controller;

import ac.knu.likeknu.controller.dto.base.ResponseDto;
import ac.knu.likeknu.controller.dto.suggestion.request.SuggestionRequest;
import ac.knu.likeknu.service.SuggestionService;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/api/suggestions")
@RestController
public class SuggestionController {

private final SuggestionService suggestionService;

public SuggestionController(SuggestionService suggestionService) {
this.suggestionService = suggestionService;
}

@PostMapping
public ResponseDto<String> registerSuggestion(@RequestBody @Valid SuggestionRequest request) {
suggestionService.createNewSuggestion(request);
return ResponseDto.of("Your offer has been registered.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ac.knu.likeknu.controller.dto.suggestion.request;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

public record SuggestionRequest(
@NotBlank String deviceId,
@NotBlank @Size(min = 1, max = 500) String content
) {
}
46 changes: 46 additions & 0 deletions src/main/java/ac/knu/likeknu/domain/Suggestion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ac.knu.likeknu.domain;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;

@Getter
@Table(name = "suggestion")
@Entity
public class Suggestion {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String content;

private LocalDateTime createdAt;

@JoinColumn(name = "device")
@ManyToOne(fetch = FetchType.LAZY)
private Device device;

protected Suggestion() {
}

@Builder
public Suggestion(String content, LocalDateTime createdAt, Device device) {
this.content = content;
this.createdAt = createdAt;
this.device = device;
}

public static Suggestion of(Device device, String content) {
return new Suggestion(content, LocalDateTime.now(), device);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ac.knu.likeknu.repository;

import ac.knu.likeknu.domain.Suggestion;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SuggestionRepository extends JpaRepository<Suggestion, Long> {
}
31 changes: 31 additions & 0 deletions src/main/java/ac/knu/likeknu/service/SuggestionService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ac.knu.likeknu.service;

import ac.knu.likeknu.controller.dto.suggestion.request.SuggestionRequest;
import ac.knu.likeknu.domain.Device;
import ac.knu.likeknu.domain.Suggestion;
import ac.knu.likeknu.exception.BusinessException;
import ac.knu.likeknu.repository.DeviceRepository;
import ac.knu.likeknu.repository.SuggestionRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class SuggestionService {

private final DeviceRepository deviceRepository;
private final SuggestionRepository suggestionRepository;

public SuggestionService(DeviceRepository deviceRepository, SuggestionRepository suggestionRepository) {
this.deviceRepository = deviceRepository;
this.suggestionRepository = suggestionRepository;
}

public void createNewSuggestion(SuggestionRequest request) {
String deviceId = request.deviceId();
Device device = deviceRepository.findById(deviceId)
.orElseThrow(() -> new BusinessException(String.format("Device not found! [%s]", deviceId)));
Suggestion suggestion = Suggestion.of(device, request.content());
suggestionRepository.save(suggestion);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS suggestion
(
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
content VARCHAR(500) NOT NULL,
created_at DATETIME NOT NULL,
device VARCHAR(60),
FOREIGN KEY (device) REFERENCES device (id) ON DELETE SET NULL ON UPDATE SET NULL
);

0 comments on commit 71ead9d

Please sign in to comment.