Fakultas Ilmu Komputer UI

Commit 5f931e01 authored by SYAHDAN PUTRA ADRIATAMA's avatar SYAHDAN PUTRA ADRIATAMA
Browse files

Merge branch 'task-implementation-course' into 'master'

Implement course page

See merge request !3
parents ef740150 c26519ed
package id.ac.ui.cs.advprog.tutorial0.controller;
import id.ac.ui.cs.advprog.tutorial0.model.Course;
import id.ac.ui.cs.advprog.tutorial0.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@RequestMapping("/course")
public class CourseController {
@Autowired
private CourseService service;
@GetMapping("/list")
public String courseListPage(Model model) {
List<Course> allCourses = service.findAll();
model.addAttribute("courses", allCourses);
return "courseList";
}
@GetMapping("/create")
public String createCoursePage(Model model) {
Course course = new Course();
model.addAttribute("course", course);
return "createCourse";
}
@PostMapping("/create")
public String createCoursePost(@ModelAttribute Course course, Model model) {
service.create(course);
return "redirect:list";
}
}
package id.ac.ui.cs.advprog.tutorial0.model;
import lombok.Getter;
import lombok.Setter;
@Getter @Setter
public class Course {
private String courseId;
private String name;
private Boolean status = true;
}
package id.ac.ui.cs.advprog.tutorial0.repository;
import id.ac.ui.cs.advprog.tutorial0.model.Course;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Repository
public class CourseRepository {
private List<Course> coursesInMemory = new ArrayList<>();
public Course create(Course course) {
coursesInMemory.add(course);
return course;
}
public Iterator<Course> findAll() {
return coursesInMemory.iterator();
}
}
package id.ac.ui.cs.advprog.tutorial0.service;
import id.ac.ui.cs.advprog.tutorial0.model.Course;
import java.util.List;
public interface CourseService {
public Course create(Course course);
public List<Course> findAll();
}
package id.ac.ui.cs.advprog.tutorial0.service;
import id.ac.ui.cs.advprog.tutorial0.model.Course;
import id.ac.ui.cs.advprog.tutorial0.repository.CourseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Service
public class CourseServiceImpl implements CourseService {
@Autowired
private CourseRepository courseRepository;
@Override
public Course create(Course course) {
generateId(course);
courseRepository.create(course);
return course;
}
private void generateId(Course course) {
StringBuilder stringBuilder = new StringBuilder();
for (char letter: course.getName().toCharArray()) {
stringBuilder.append(String.valueOf((int) letter));
}
String id = stringBuilder.toString();
course.setCourseId(id);
}
@Override
public List<Course> findAll() {
Iterator<Course> courseIterator = courseRepository.findAll();
List<Course> allCourse = new ArrayList<>();
courseIterator.forEachRemaining(allCourse::add);
return allCourse;
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Course List</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<h2>Course' List</h2>
<a th:href="@{/course/create}" class="btn btn-primary btn-sm mb-3" >Create course</a>
<table class="table">
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Course Id</th>
<th scope="col">Vacancy Status</th>
</tr>
</thead>
<tbody th:each="course: ${courses}">
<tr>
<td th:text="${course.name}"></td>
<td th:text="${course.courseId}"></td>
<td th:text="${course.status}"></td>
</tr>
</tbody>
</table>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Create new Course</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<h3>Create new Course</h3>
<form th:action="@{/course/create}" th:object="${course}" method="post">
<div class="form-group">
<label for="nameInput">Course Name</label>
<input th:field="*{name}" type="text" class="form-control" id="nameInput" aria-describedby="nameHelp" placeholder="Enter course's name" required>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
</body>
</html>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment