Skip to content

Commit

Permalink
[feat] 타임리프 템플릿 Admin Page 세팅 구현 - #214
Browse files Browse the repository at this point in the history
  • Loading branch information
rlarlgnszx committed Jul 30, 2024
1 parent 8740678 commit 3ae93c3
Show file tree
Hide file tree
Showing 11 changed files with 601 additions and 0 deletions.
140 changes: 140 additions & 0 deletions dateroad-api/src/main/java/org/dateroad/admin/AdminController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package org.dateroad.admin;

import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import lombok.RequiredArgsConstructor;
import org.dateroad.advertisement.domain.Advertisement;
import org.dateroad.date.domain.Course;
import org.dateroad.date.domain.Date;
import org.dateroad.point.domain.Point;
import org.dateroad.user.domain.User;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

// AdminController.java
@Controller
@RequestMapping("/admin")
@RequiredArgsConstructor
public class AdminController {
private final AdminService adminService;
private final ResourceLoader resourceLoader;

@GetMapping
public String dashboard(Model model) throws IOException {
model.addAttribute("courses", adminService.getAllCourses());
model.addAttribute("dates", adminService.getAllDates());
model.addAttribute("users", adminService.getAllUsers());
model.addAttribute("advertisements", adminService.getAllAdvertisements());
model.addAttribute("points", adminService.getAllPoints());

Resource resource = resourceLoader.getResource("classpath:static/img.png");
InputStream inputStream = resource.getInputStream();
byte[] imageBytes = inputStream.readAllBytes();
String imageBase64 = Base64.getEncoder().encodeToString(imageBytes);
model.addAttribute("imageBase64", imageBase64);
return "admin/dashboard";
}

// Courses
@GetMapping("/courses")
public String listCourses(Model model) {
model.addAttribute("courses", adminService.getAllCourses());
return "admin/courses";
}

@GetMapping("/courses/edit/{id}")
public String editCourse(@PathVariable Long id, Model model) {
model.addAttribute("course", adminService.getCourseById(id));
return "admin/edit-course";
}

@PostMapping("/courses/update/{id}")
public String updateCourse(@PathVariable Long id, @ModelAttribute Course course) {
adminService.updateCourse(id, course);
return "redirect:/admin/courses";
}

// Dates
@GetMapping("/dates")
public String listDates(Model model) {
model.addAttribute("dates", adminService.getAllDates());
return "admin/dates";
}

@GetMapping("/dates/edit/{id}")
public String editDate(@PathVariable Long id, Model model) {
model.addAttribute("date", adminService.getDateById(id));
return "admin/edit-date";
}

@PostMapping("/dates/update/{id}")
public String updateDate(@PathVariable Long id, @ModelAttribute Date date) {
adminService.updateDate(id, date);
return "redirect:/admin/dates";
}

// Users
@GetMapping("/users")
public String listUsers(Model model) {
model.addAttribute("users", adminService.getAllUsers());
return "admin/users";
}

@GetMapping("/users/edit/{id}")
public String editUser(@PathVariable Long id, Model model) {
model.addAttribute("user", adminService.getUserById(id));
return "admin/edit-user";
}

@PostMapping("/users/update/{id}")
public String updateUser(@PathVariable Long id, @ModelAttribute User user) {
adminService.updateUser(id, user);
return "redirect:/admin/users";
}

// Advertisements
@GetMapping("/advertisements")
public String listAdvertisements(Model model) {
model.addAttribute("advertisements", adminService.getAllAdvertisements());
return "admin/advertisements";
}

@GetMapping("/advertisements/edit/{id}")
public String editAdvertisement(@PathVariable Long id, Model model) {
model.addAttribute("advertisement", adminService.getAdvertisementById(id));
return "admin/edit-advertisement";
}

@PostMapping("/advertisements/update/{id}")
public String updateAdvertisement(@PathVariable Long id, @ModelAttribute Advertisement advertisement) {
adminService.updateAdvertisement(id, advertisement);
return "redirect:/admin/advertisements";
}

// Points
@GetMapping("/points")
public String listPoints(Model model) {
model.addAttribute("points", adminService.getAllPoints());
return "admin/points";
}

@GetMapping("/points/edit/{id}")
public String editPoint(@PathVariable Long id, Model model) {
model.addAttribute("point", adminService.getPointById(id));
return "admin/edit-point";
}

@PostMapping("/points/update/{id}")
public String updatePoint(@PathVariable Long id, @ModelAttribute Point point) {
adminService.updatePoint(id, point);
return "redirect:/admin/points";
}
}
117 changes: 117 additions & 0 deletions dateroad-api/src/main/java/org/dateroad/admin/AdminService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package org.dateroad.admin;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.dateroad.advertisement.domain.Advertisement;
import org.dateroad.advertisement.repository.AdvertisementRepository;
import org.dateroad.date.domain.Course;
import org.dateroad.date.domain.Date;
import org.dateroad.date.repository.CourseRepository;
import org.dateroad.date.service.DateRepository;
import org.dateroad.exception.EntityNotFoundException;
import org.dateroad.point.domain.Point;
import org.dateroad.point.repository.PointRepository;
import org.dateroad.user.domain.User;
import org.dateroad.user.repository.UserRepository;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class AdminService {

private final CourseRepository courseRepository;

private final DateRepository dateRepository;

private final UserRepository userRepository;

private final AdvertisementRepository advertisementRepository;

private final PointRepository pointRepository;

// Courses
public List<Course> getAllCourses() {
return courseRepository.findAll();
}

public Course getCourseById(Long id) {
return courseRepository.findById(id).orElseThrow(EntityNotFoundException::new);
}

public void updateCourse(Long id, Course updatedCourse) {
Course course = getCourseById(id);
// course.setTitle(updatedCourse.getTitle());
// course.setStartAt(updatedCourse.getStartAt());
// course.setCity(updatedCourse.getCity());
// course.setCost(updatedCourse.getCost());
courseRepository.save(course);
}

// Dates
public List<Date> getAllDates() {
return dateRepository.findAll();
}

public Date getDateById(Long id) {
return dateRepository.findById(id).orElseThrow(EntityNotFoundException::new);
}

public void updateDate(Long id, Date updatedDate) {
Date date = getDateById(id);
// date.setTitle(updatedDate.getTitle());
// date.setDate(updatedDate.getDate());
// date.setStartAt(updatedDate.getStartAt());
// date.setCity(updatedDate.getCity());
dateRepository.save(date);
}

// Users
public List<User> getAllUsers() {
return userRepository.findAll();
}

public User getUserById(Long id) {
return userRepository.findById(id).orElseThrow(EntityNotFoundException::new);
}

public void updateUser(Long id, User updatedUser) {
User user = getUserById(id);
user.setName(updatedUser.getName());
userRepository.save(user);
}

// Advertisements
public List<Advertisement> getAllAdvertisements() {
return advertisementRepository.findAll();
}

public Advertisement getAdvertisementById(Long id) {
return advertisementRepository.findById(id).orElseThrow(EntityNotFoundException::new);
}

public void updateAdvertisement(Long id, Advertisement updatedAdvertisement) {
Advertisement advertisement = getAdvertisementById(id);
// advertisement.setTitle(updatedAdvertisement.getTitle());
// advertisement.setDescription(updatedAdvertisement.getDescription());
advertisement.setThumbnail(updatedAdvertisement.getThumbnail());
// advertisement.setTag(updatedAdvertisement.getTag());
advertisementRepository.save(advertisement);
}

// Points
public List<Point> getAllPoints() {
return pointRepository.findAll();
}

public Point getPointById(Long id) {
return pointRepository.findById(id).orElseThrow(EntityNotFoundException::new);
}

public void updatePoint(Long id, Point updatedPoint) {
Point point = getPointById(id);
// point.setPoint(updatedPoint.getPoint());
// point.setTransactionType(updatedPoint.getTransactionType());
// point.setDescription(updatedPoint.getDescription());
pointRepository.save(point);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Advertisements</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css">
</head>
<body class="bg-gray-100">
<div class="container mx-auto px-6 py-8">
<h2 class="text-2xl font-semibold text-gray-800">Advertisements</h2>
<div class="mt-8">
<table class="min-w-full bg-white">
<thead>
<tr>
<th class="px-6 py-3 border-b-2 border-gray-300 text-left text-sm leading-4 text-gray-500 uppercase tracking-wider">Title</th>
<th class="px-6 py-3 border-b-2 border-gray-300 text-left text-sm leading-4 text-gray-500 uppercase tracking-wider">Description</th>
<th class="px-6 py-3 border-b-2 border-gray-300 text-left text-sm leading-4 text-gray-500 uppercase tracking-wider">Thumbnail</th>
<th class="px-6 py-3 border-b-2 border-gray-300 text-left text-sm leading-4 text-gray-500 uppercase tracking-wider">Tag</th>
<th class="px-6 py-3 border-b-2 border-gray-300 text-left text-sm leading-4 text-gray-500 uppercase tracking-wider">Actions</th>
</tr>
</thead>
<tbody>
<tr th:each="advertisement : ${advertisements}">
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-500">
<div class="text-sm leading-5 text-gray-900" th:text="${advertisement.title}"></div>
</td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-500">
<div class="text-sm leading-5 text-gray-900" th:text="${advertisement.description}"></div>
</td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-500">
<div class="text-sm leading-5 text-gray-900" th:text="${advertisement.thumbnail}"></div>
</td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-500">
<div class="text-sm leading-5 text-gray-900" th:text="${advertisement.tag}"></div>
</td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-500">
<a href="#" th:href="@{/advertisements/edit/{id}(id=${advertisement.id})}" class="text-indigo-600 hover:text-indigo-900">Edit</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
52 changes: 52 additions & 0 deletions dateroad-api/src/main/resources/templates/admin/courses.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Courses</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css">
</head>
<body class="bg-gray-100">
<div class="container mx-auto px-6 py-8">
<h2 class="text-2xl font-semibold text-gray-800">Courses</h2>
<div class="mt-8">
<table class="min-w-full bg-white">
<thead>
<tr>
<th class="px-6 py-3 border-b-2 border-gray-300 text-left text-sm leading-4 text-gray-500 uppercase tracking-wider">Title</th>
<th class="px-6 py-3 border-b-2 border-gray-300 text-left text-sm leading-4 text-gray-500 uppercase tracking-wider">Start Date</th>
<th class="px-6 py-3 border-b-2 border-gray-300 text-left text-sm leading-4 text-gray-500 uppercase tracking-wider">Location</th>
<th class="px-6 py-3 border-b-2 border-gray-300 text-left text-sm leading-4 text-gray-500 uppercase tracking-wider">Price</th>
<th class="px-6 py-3 border-b-2 border-gray-300 text-left text-sm leading-4 text-gray-500 uppercase tracking-wider">썸네일</th>
<th class="px-6 py-3 border-b-2 border-gray-300 text-left text-sm leading-4 text-gray-500 uppercase tracking-wider">만든 사람</th>
<th class="px-6 py-3 border-b-2 border-gray-300 text-left text-sm leading-4 text-gray-500 uppercase tracking-wider">Actions</th>
</tr>
</thead>
<tbody>
<tr th:each="course : ${courses}">
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-500">
<div class="text-sm leading-5 text-gray-900" th:text="${course.title}"></div>
</td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-500">
<div class="text-sm leading-5 text-gray-900" th:text="${#temporals.format(course.startAt, 'yyyy.MM.dd')}"></div>
</td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-500">
<div class="text-sm leading-5 text-gray-900" th:text="${course.city.name()}"></div>
</td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-500">
<div class="text-sm leading-5 text-gray-900" th:text="${course.getCost()} + 원"></div>
</td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-500">
<div class="text-sm leading-5 text-gray-900" th:text="${course.getThumbnail()}"></div>
</td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-500">
<div class="text-sm leading-5 text-gray-900" th:text="${course.getUser().getName()}"></div>
</td>
<td class="px-6 py-4 whitespace-no-wrap border-b border-gray-500">
<a href="#" th:href="@{/courses/edit/{id}(id=${course.id})}" class="text-indigo-600 hover:text-indigo-900">Edit</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>
Loading

0 comments on commit 3ae93c3

Please sign in to comment.