Skip to content

Commit

Permalink
[feat] 대학교 추가 API 구현 (#214)
Browse files Browse the repository at this point in the history
* [feat] make command, request DTO

* [feat] add static method in UniversityEntity

* [feat] implements controller

* [feat] implements service

* [chore] update submodule

* [refactor] change package

* [chore] update submodule

* [refactor] add a line break
  • Loading branch information
PicturePark1101 authored Nov 12, 2024
1 parent 151dba5 commit 04e70b6
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 2 deletions.
2 changes: 1 addition & 1 deletion server-yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.api.dto.HankkiResponse;
import org.hankki.hankkiserver.api.university.controller.request.UniversitiesPostRequest;
import org.hankki.hankkiserver.api.university.service.UniversityCommandService;
import org.hankki.hankkiserver.api.university.service.UniversityQueryService;
import org.hankki.hankkiserver.api.university.service.command.UniversitiesPostCommand;
import org.hankki.hankkiserver.api.university.service.response.UniversitiesResponse;
import org.hankki.hankkiserver.common.code.CommonSuccessCode;
import org.springframework.web.bind.annotation.GetMapping;
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;

Expand All @@ -15,9 +20,16 @@
public class UniversityController {

private final UniversityQueryService universityQueryService;
private final UniversityCommandService universityCommandService;

@GetMapping("/universities")
public HankkiResponse<UniversitiesResponse> getUniversities() {
return HankkiResponse.success(CommonSuccessCode.OK, universityQueryService.findAllByOrderByName());
}

@PostMapping("/universities")
public HankkiResponse<Void> createUniversities(@RequestBody UniversitiesPostRequest universitiesPostRequest) {
universityCommandService.createUniversities(UniversitiesPostCommand.of(universitiesPostRequest));
return HankkiResponse.success(CommonSuccessCode.CREATED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.hankki.hankkiserver.api.university.controller.request;

import java.util.List;

public record UniversitiesPostRequest(List<UniversityRequest> universities) {
public record UniversityRequest(String name, double longitude, double latitude) { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.hankki.hankkiserver.api.university.service;

import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.api.university.service.command.UniversitiesPostCommand;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class UniversityCommandService {

private final UniversityUpdater universityUpdater;

@Transactional
public void createUniversities(UniversitiesPostCommand universitiesPostCommand) {
universityUpdater.saveAll(universitiesPostCommand.toEntityList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.hankki.hankkiserver.api.university.service;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.domain.university.model.University;
import org.hankki.hankkiserver.domain.university.repository.UniversityRepository;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class UniversityUpdater {

private final UniversityRepository universityRepository;

void saveAll(List<University> universities){
universityRepository.saveAll(universities);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.hankki.hankkiserver.api.university.service.command;

import java.util.List;
import org.hankki.hankkiserver.api.university.controller.request.UniversitiesPostRequest;
import org.hankki.hankkiserver.domain.common.Point;
import org.hankki.hankkiserver.domain.university.model.University;

public record UniversitiesPostCommand(List<UniversityCommand> universityCommands) {

record UniversityCommand(String name, double longitude, double latitude) {
University toEntity() {
return University.create(name, new Point(latitude, longitude));
}
}

public static UniversitiesPostCommand of(UniversitiesPostRequest universitiesPostRequest) {
return new UniversitiesPostCommand(universitiesPostRequest.universities().stream().map(it -> new UniversityCommand(it.name(), it.longitude(), it.latitude())).toList());
}

public List<University> toEntityList() {
return universityCommands.stream().map(UniversityCommand::toEntity).toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package org.hankki.hankkiserver.domain.university.model;

import jakarta.persistence.*;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hankki.hankkiserver.domain.common.Point;
Expand All @@ -22,4 +28,16 @@ public class University {
@Column(nullable = false)
private String name;

public static University create(String name, Point point) {
return University.builder()
.name(name)
.point(point)
.build();
}

@Builder
private University(String name, Point point) {
this.name = name;
this.point = point;
}
}

0 comments on commit 04e70b6

Please sign in to comment.