Skip to content

Commit

Permalink
Merge pull request #76 from DSM-Repo/history
Browse files Browse the repository at this point in the history
  • Loading branch information
ori0o0p authored Aug 25, 2024
2 parents 7aaad79 + 00d9f99 commit b7b1f5e
Show file tree
Hide file tree
Showing 15 changed files with 222 additions and 0 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ dependencies {

// Swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2'

// Mapstruct
implementation 'org.mapstruct:mapstruct:1.6.0'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.6.0'

}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.whopper.application.history.impl;

import com.example.whopper.application.history.usecase.CreateHistoryUseCase;
import com.example.whopper.domain.history.HistoryModel;
import com.example.whopper.domain.history.HistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
class CreateHistoryService implements CreateHistoryUseCase {
private final HistoryRepository historyRepository;

@Override
public void create(String date, String content) {
final var newModel = new HistoryModel(date, content);

historyRepository.save(newModel);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.whopper.application.history.impl;

import com.example.whopper.application.history.usecase.DeleteHistoryUseCase;
import com.example.whopper.domain.history.HistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
class DeleteHistoryService implements DeleteHistoryUseCase {
private final HistoryRepository historyRepository;

@Override
public void deleteById(String historyId) {
historyRepository.deleteById(historyId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.whopper.application.history.impl;

import com.example.whopper.application.history.usecase.ViewHistoryUseCase;
import com.example.whopper.domain.history.HistoryRepository;
import com.example.whopper.interfaces.history.dto.HistoryResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
class ViewHistoryService implements ViewHistoryUseCase {
private final HistoryRepository historyRepository;

@Override
public List<HistoryResponse> viewAll() {
return historyRepository.findAll()
.map(history -> new HistoryResponse(history.id(), history.date(), history.content()))
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.whopper.application.history.usecase;

public interface CreateHistoryUseCase {
void create(String date, String content);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.whopper.application.history.usecase;

public interface DeleteHistoryUseCase {
void deleteById(String historyId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.whopper.application.history.usecase;

import com.example.whopper.interfaces.history.dto.HistoryResponse;

import java.util.List;

public interface ViewHistoryUseCase {
List<HistoryResponse> viewAll();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.whopper.domain.history;

import org.springframework.data.repository.Repository;

import java.util.stream.Stream;

interface HistoryDataRepository extends Repository<HistoryEntity, String> {
Stream<HistoryEntity> findAll();
void save(HistoryEntity entity);
void deleteById(String id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.whopper.domain.history;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document("history_repo")
@Getter(value = AccessLevel.PACKAGE)
@AllArgsConstructor(access = AccessLevel.PACKAGE)
class HistoryEntity {
@Id
private String id;

private String date;

private String content;

protected HistoryEntity() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.example.whopper.domain.history;

import org.mapstruct.Mapper;
import org.mapstruct.ReportingPolicy;

@Mapper(componentModel = "spring", unmappedSourcePolicy = ReportingPolicy.IGNORE)
public interface HistoryEntityMapper {
HistoryModel toModel(HistoryEntity entity);
HistoryEntity toEntity(HistoryModel model);
}
14 changes: 14 additions & 0 deletions src/main/java/com/example/whopper/domain/history/HistoryModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.whopper.domain.history;

public record HistoryModel(
String id,
String date,
String content
) {
public HistoryModel(String date, String content) {
this(null, date, content);
}
public HistoryModel update(String date, String content) {
return new HistoryModel(id, date, content);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.whopper.domain.history;

import java.util.stream.Stream;

public interface HistoryRepository {
void save(HistoryModel model);
void deleteById(String historyId);
Stream<HistoryModel> findAll();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.whopper.domain.history;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.stream.Stream;

@Repository
@RequiredArgsConstructor
class HistoryRepositoryImpl implements HistoryRepository {
private final HistoryDataRepository historyDataRepository;
private final HistoryEntityMapper historyEntityMapper;

@Override
public void save(HistoryModel model) {
historyDataRepository.save(historyEntityMapper.toEntity(model));
}

@Override
public void deleteById(String historyId) {
historyDataRepository.deleteById(historyId);
}

@Override
public Stream<HistoryModel> findAll() {
return historyDataRepository.findAll()
.map(historyEntityMapper::toModel);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.whopper.interfaces.history;

import com.example.whopper.application.history.usecase.CreateHistoryUseCase;
import com.example.whopper.application.history.usecase.DeleteHistoryUseCase;
import com.example.whopper.application.history.usecase.ViewHistoryUseCase;
import com.example.whopper.common.annotation.OnlyTeacher;
import com.example.whopper.interfaces.history.dto.HistoryResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/history")
class HistoryController {
private final CreateHistoryUseCase createHistoryUseCase;
private final ViewHistoryUseCase viewHistoryUseCase;
private final DeleteHistoryUseCase deleteHistoryUseCase;

@OnlyTeacher
@GetMapping
List<HistoryResponse> viewAll() {
return viewHistoryUseCase.viewAll();
}

@OnlyTeacher
@PostMapping
void create(@RequestBody CreateHistoryRequest request) {
createHistoryUseCase.create(request.date(), request.content());
}

@OnlyTeacher
@PutMapping("/del")
void deleteById(@RequestBody DeleteHistoryRequest request) {
deleteHistoryUseCase.deleteById(request.id());
}

record DeleteHistoryRequest(String id) {}
record CreateHistoryRequest(String date, String content) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.whopper.interfaces.history.dto;

public record HistoryResponse(String id, String date, String content) {
}

0 comments on commit b7b1f5e

Please sign in to comment.