diff --git a/build.gradle b/build.gradle index 6d46da9..70e0d51 100644 --- a/build.gradle +++ b/build.gradle @@ -17,6 +17,12 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter' + + // basic spring web package dependency + implementation 'org.springframework.boot:spring-boot-starter-web' + // dependency for dynamic webpage + implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' + testImplementation 'org.springframework.boot:spring-boot-starter-test' } diff --git a/src/main/java/com/softgallery/profilegameserverdemo/controller/GithubAPIController.java b/src/main/java/com/softgallery/profilegameserverdemo/controller/GithubAPIController.java new file mode 100644 index 0000000..6ac8f21 --- /dev/null +++ b/src/main/java/com/softgallery/profilegameserverdemo/controller/GithubAPIController.java @@ -0,0 +1,19 @@ +package com.softgallery.profilegameserverdemo.controller; + +import com.softgallery.profilegameserverdemo.service.GithubAPIService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class GithubAPIController { + private final GithubAPIService githubAPIService = new GithubAPIService(); + + @GetMapping("/commit/{name}") + public String getCommitInfoByName(@PathVariable("name") String name) { + System.out.println(name); + String repo = "spring-roomescape-playground"; // repo 이름을 수정하여 넣을 것 + + return githubAPIService.getCommits(name, repo); + } +} diff --git a/src/main/java/com/softgallery/profilegameserverdemo/controller/MainController.java b/src/main/java/com/softgallery/profilegameserverdemo/controller/MainController.java new file mode 100644 index 0000000..ab69a35 --- /dev/null +++ b/src/main/java/com/softgallery/profilegameserverdemo/controller/MainController.java @@ -0,0 +1,31 @@ +package com.softgallery.profilegameserverdemo.controller; + +import com.softgallery.profilegameserverdemo.domain.Profile; +import com.softgallery.profilegameserverdemo.service.ProfileService; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +@Controller +public class MainController { + private final ProfileService profileService = new ProfileService(); + + @GetMapping("/") + public String test() { return "test"; } + + @GetMapping("/{name}") + @ResponseBody + public Profile searchProfile(@PathVariable("name") String name) { + System.out.println(name); + + return profileService.getProfile(name); + } + + @GetMapping("/profile") + @ResponseBody + public Profile printProfile() { + return new Profile(0L, "Jaehoon", "00:01:02", "2024-01-01"); + } +} diff --git a/src/main/java/com/softgallery/profilegameserverdemo/domain/Profile.java b/src/main/java/com/softgallery/profilegameserverdemo/domain/Profile.java new file mode 100644 index 0000000..bc0e72c --- /dev/null +++ b/src/main/java/com/softgallery/profilegameserverdemo/domain/Profile.java @@ -0,0 +1,34 @@ +package com.softgallery.profilegameserverdemo.domain; + +import java.time.LocalDate; +import java.time.LocalTime; + +public class Profile { + private final Long id; + private final String name; + private final LocalTime recentTime; + private final LocalDate recentDate; + + public Profile(final Long id, final String name, final String time, final String date) { + this.id = id; + this.name = name; + this.recentTime = LocalTime.parse(time); + this.recentDate = LocalDate.parse(date); + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public LocalTime getRecentTime() { + return recentTime; + } + + public LocalDate getRecentDate() { + return recentDate; + } +} diff --git a/src/main/java/com/softgallery/profilegameserverdemo/service/GithubAPIService.java b/src/main/java/com/softgallery/profilegameserverdemo/service/GithubAPIService.java new file mode 100644 index 0000000..8bc12a8 --- /dev/null +++ b/src/main/java/com/softgallery/profilegameserverdemo/service/GithubAPIService.java @@ -0,0 +1,18 @@ +package com.softgallery.profilegameserverdemo.service; + +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +@Service +public class GithubAPIService { + private final String GITHUB_API_URL = "https://api.github.com"; + + private final RestTemplate restTemplate = new RestTemplate(); + + public GithubAPIService() { } + + public String getCommits(String owner, String repo) { + String url = GITHUB_API_URL + "/repos/" + owner + "/" + repo + "/commits"; + return restTemplate.getForObject(url, String.class); + } +} diff --git a/src/main/java/com/softgallery/profilegameserverdemo/service/ProfileService.java b/src/main/java/com/softgallery/profilegameserverdemo/service/ProfileService.java new file mode 100644 index 0000000..41f7208 --- /dev/null +++ b/src/main/java/com/softgallery/profilegameserverdemo/service/ProfileService.java @@ -0,0 +1,34 @@ +package com.softgallery.profilegameserverdemo.service; + +import com.softgallery.profilegameserverdemo.domain.Profile; +import java.util.ArrayList; + +public class ProfileService { + private final ArrayList profiles = new ArrayList<>(); + + public ProfileService() { + initialize(); + } + + // Initialization with test-cases + private void initialize() { + profiles.add(new Profile(1L, "Jaehoon", "00:01:02", "2024-01-01")); + profiles.add(new Profile(2L, "SHKim55", "23:00:00", "2024-12-23")); + profiles.add(new Profile(3L, "Sumin", "22:00:00", "2023-12-23")); + profiles.add(new Profile(4L, "YongWoo", "21:00:00", "2023-11-23")); + profiles.add(new Profile(5L, "Hangyul", "20:00:00", "2023-10-23")); + } + + public ArrayList getAllProfiles() { + return profiles; + } + + // Find profile by name + public Profile getProfile(String name) { + for(Profile profile : profiles) { + if (profile.getName().equals(name)) + return profile; + } + return new Profile(0L, "No Such User", "00:00:00", "0000-00-00"); + } +} \ No newline at end of file diff --git a/src/main/resources/templates/test.html b/src/main/resources/templates/test.html new file mode 100644 index 0000000..b4f869e --- /dev/null +++ b/src/main/resources/templates/test.html @@ -0,0 +1,10 @@ + + + + + Test Page + + + Testing + + \ No newline at end of file