-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/ac/knu/likeknu/controller/SuggestionController.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 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."); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/ac/knu/likeknu/controller/dto/suggestion/request/SuggestionRequest.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,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 | ||
) { | ||
} |
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,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); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/ac/knu/likeknu/repository/SuggestionRepository.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 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
31
src/main/java/ac/knu/likeknu/service/SuggestionService.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,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); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/resources/db/migration/mysql/V11__add_suggestion_table.sql
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,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 | ||
); |