From 8ed3891b44cada09a51c8e78f1119bbcb73fb829 Mon Sep 17 00:00:00 2001
From: "pramudya.wibisono" <pramudya.wibisono@ui.ac.id>
Date: Tue, 15 Feb 2022 07:46:30 +0700
Subject: [PATCH 1/3] Implement student page

---
 .../controller/StudentController.java         | 46 ++++++++++++++++
 .../DuplicateStudentNameException.java        |  8 +++
 .../id/ac/ui/tutorial0/model/Student.java     | 11 ++++
 .../repository/StudentRepository.java         | 23 ++++++++
 .../ui/tutorial0/service/StudentService.java  | 11 ++++
 .../tutorial0/service/StudentServiceImpl.java | 53 +++++++++++++++++++
 .../resources/templates/createStudent.html    | 27 ++++++++++
 src/main/resources/templates/studentList.html | 34 ++++++++++++
 8 files changed, 213 insertions(+)
 create mode 100644 src/main/java/id/ac/ui/tutorial0/controller/StudentController.java
 create mode 100644 src/main/java/id/ac/ui/tutorial0/exception/DuplicateStudentNameException.java
 create mode 100644 src/main/java/id/ac/ui/tutorial0/model/Student.java
 create mode 100644 src/main/java/id/ac/ui/tutorial0/repository/StudentRepository.java
 create mode 100644 src/main/java/id/ac/ui/tutorial0/service/StudentService.java
 create mode 100644 src/main/java/id/ac/ui/tutorial0/service/StudentServiceImpl.java
 create mode 100644 src/main/resources/templates/createStudent.html
 create mode 100644 src/main/resources/templates/studentList.html

diff --git a/src/main/java/id/ac/ui/tutorial0/controller/StudentController.java b/src/main/java/id/ac/ui/tutorial0/controller/StudentController.java
new file mode 100644
index 0000000..79b895d
--- /dev/null
+++ b/src/main/java/id/ac/ui/tutorial0/controller/StudentController.java
@@ -0,0 +1,46 @@
+package id.ac.ui.tutorial0.controller;
+
+import id.ac.ui.tutorial0.exception.DuplicateStudentNameException;
+import id.ac.ui.tutorial0.model.Student;
+import id.ac.ui.tutorial0.service.StudentService;
+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("/student")
+public class StudentController {
+
+    @Autowired
+    private StudentService service;
+
+    @GetMapping("/list")
+    public String studentListPage(Model model) {
+        List<Student> allStudents = service.findAll();
+        model.addAttribute("students", allStudents);
+        return "studentList";
+    }
+
+    @GetMapping("/create")
+    public String createStudentPage(Model model) {
+        Student student = new Student();
+        model.addAttribute("student", student);
+        return "createStudent";
+    }
+
+    @PostMapping("/create")
+    public String createStudentPost(@ModelAttribute Student student, Model model) {
+        try {
+            service.create(student);
+        } catch (DuplicateStudentNameException e) {
+            model.addAttribute("error", e);
+            model.addAttribute("student", student);
+            return "createStudent";
+        }
+        return "redirect:list";
+    }
+
+}
\ No newline at end of file
diff --git a/src/main/java/id/ac/ui/tutorial0/exception/DuplicateStudentNameException.java b/src/main/java/id/ac/ui/tutorial0/exception/DuplicateStudentNameException.java
new file mode 100644
index 0000000..ede42ac
--- /dev/null
+++ b/src/main/java/id/ac/ui/tutorial0/exception/DuplicateStudentNameException.java
@@ -0,0 +1,8 @@
+package id.ac.ui.tutorial0.exception;
+
+public class DuplicateStudentNameException extends RuntimeException {
+
+    public DuplicateStudentNameException(String studentName) {
+        super(String.format("The student name %s is a duplicate!",studentName));
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/id/ac/ui/tutorial0/model/Student.java b/src/main/java/id/ac/ui/tutorial0/model/Student.java
new file mode 100644
index 0000000..366c7aa
--- /dev/null
+++ b/src/main/java/id/ac/ui/tutorial0/model/Student.java
@@ -0,0 +1,11 @@
+package id.ac.ui.tutorial0.model;
+
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter @Setter
+public class Student {
+    private String name;
+    private String npm;
+    private String address;
+}
diff --git a/src/main/java/id/ac/ui/tutorial0/repository/StudentRepository.java b/src/main/java/id/ac/ui/tutorial0/repository/StudentRepository.java
new file mode 100644
index 0000000..8a45d5c
--- /dev/null
+++ b/src/main/java/id/ac/ui/tutorial0/repository/StudentRepository.java
@@ -0,0 +1,23 @@
+package id.ac.ui.tutorial0.repository;
+
+import id.ac.ui.tutorial0.model.Student;
+import org.springframework.stereotype.Repository;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+@Repository
+public class StudentRepository {
+
+    private List<Student> studentsInMemory = new ArrayList<>();
+
+    public Student create(Student student) {
+        studentsInMemory.add(student);
+        return student;
+    }
+
+    public Iterator<Student> findAll() {
+        return studentsInMemory.iterator();
+    }
+}
diff --git a/src/main/java/id/ac/ui/tutorial0/service/StudentService.java b/src/main/java/id/ac/ui/tutorial0/service/StudentService.java
new file mode 100644
index 0000000..287c251
--- /dev/null
+++ b/src/main/java/id/ac/ui/tutorial0/service/StudentService.java
@@ -0,0 +1,11 @@
+package id.ac.ui.tutorial0.service;
+
+import id.ac.ui.tutorial0.model.Student;
+
+import java.util.List;
+
+public interface StudentService {
+
+    public Student create(Student student);
+    public List<Student> findAll();
+}
diff --git a/src/main/java/id/ac/ui/tutorial0/service/StudentServiceImpl.java b/src/main/java/id/ac/ui/tutorial0/service/StudentServiceImpl.java
new file mode 100644
index 0000000..d2ab7ae
--- /dev/null
+++ b/src/main/java/id/ac/ui/tutorial0/service/StudentServiceImpl.java
@@ -0,0 +1,53 @@
+package id.ac.ui.tutorial0.service;
+
+import id.ac.ui.tutorial0.exception.DuplicateStudentNameException;
+import id.ac.ui.tutorial0.model.Student;
+import id.ac.ui.tutorial0.repository.StudentRepository;
+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 StudentServiceImpl implements StudentService {
+
+    @Autowired
+    private StudentRepository studentRepository;
+
+    @Override
+    public Student create(Student student) {
+        validateName(student);
+        generateNPM(student);
+        studentRepository.create(student);
+        return student;
+    }
+
+    private void validateName(Student student) {
+        List<Student> allStudents = findAll();
+        for(Student dbStudent: allStudents) {
+            if(dbStudent.getName().equals(student.getName())) {
+                throw new DuplicateStudentNameException(student.getName());
+            }
+        }
+
+    }
+
+    private void generateNPM(Student student) {
+        StringBuilder stringBuilder = new StringBuilder();
+        for (char letter: student.getName().toCharArray()) {
+            stringBuilder.append(String.valueOf((int)letter));
+        }
+        String npm = stringBuilder.toString();
+        student.setNpm(npm);
+    }
+
+    @Override
+    public List<Student> findAll() {
+        Iterator<Student> studentIterator = studentRepository.findAll();
+        List<Student> allStudents = new ArrayList<>();
+        studentIterator.forEachRemaining(allStudents::add);
+        return allStudents;
+    }
+}
\ No newline at end of file
diff --git a/src/main/resources/templates/createStudent.html b/src/main/resources/templates/createStudent.html
new file mode 100644
index 0000000..f142199
--- /dev/null
+++ b/src/main/resources/templates/createStudent.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<html lang="en" xmlns:th="http://www.thymeleaf.org">
+<head>
+    <meta charset="UTF-8">
+    <title>Create new Student</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 Student</h3>
+<div th:if="${error!=null}" class="alert alert-danger" role="alert">
+    Duplicate student name
+</div>
+<form th:action="@{/student/create}" th:object="${student}" method="post">
+    <div class="form-group">
+        <label for="nameInput">Name</label>
+        <input th:field="*{name}" type="text" class="form-control" id="nameInput" aria-describedby="nameHelp" placeholder="Enter student' name">
+        <small id="nameHelp" class="form-text text-muted">Please enter unique name.</small>
+    </div>
+    <div class="form-group">
+        <label for="addressTextarea">Address</label>
+        <textarea th:field="*{address}" class="form-control" id="addressTextarea" rows="3"></textarea>
+    </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
diff --git a/src/main/resources/templates/studentList.html b/src/main/resources/templates/studentList.html
new file mode 100644
index 0000000..84ab6ad
--- /dev/null
+++ b/src/main/resources/templates/studentList.html
@@ -0,0 +1,34 @@
+<!DOCTYPE html>
+<html lang="en" xmlns:th="http://www.thymeleaf.org">
+<head>
+    <meta charset="UTF-8">
+    <title>Student 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>Student' List</h2>
+<a th:href="@{/student/create}" class="btn btn-primary btn-sm mb-3" >Create student</a>
+
+<table class="table">
+    <thead>
+    <tr>
+        <th scope="col">Name</th>
+        <th scope="col">NPM</th>
+        <th scope="col">Address</th>
+    </tr>
+    </thead>
+    <tbody th:each="student: ${students}">
+    <tr>
+        <td th:text="${student.name}"></td>
+        <td th:text="${student.npm}"></td>
+        <td th:text="${student.address}"></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
-- 
GitLab


From ee9a90e23435f4b1dea619f5ded374d1781e40aa Mon Sep 17 00:00:00 2001
From: "pramudya.wibisono" <pramudya.wibisono@ui.ac.id>
Date: Tue, 15 Feb 2022 13:37:06 +0700
Subject: [PATCH 2/3] Implement course page

---
 .../controller/CourseController.java          | 38 ++++++++++++++++++
 .../java/id/ac/ui/tutorial0/model/Course.java | 12 ++++++
 .../repository/CourseRepository.java          | 23 +++++++++++
 .../ui/tutorial0/service/CourseService.java   | 11 +++++
 .../tutorial0/service/CourseServiceImpl.java  | 40 +++++++++++++++++++
 src/main/resources/templates/courseList.html  | 34 ++++++++++++++++
 .../resources/templates/createCourse.html     | 27 +++++++++++++
 7 files changed, 185 insertions(+)
 create mode 100644 src/main/java/id/ac/ui/tutorial0/controller/CourseController.java
 create mode 100644 src/main/java/id/ac/ui/tutorial0/model/Course.java
 create mode 100644 src/main/java/id/ac/ui/tutorial0/repository/CourseRepository.java
 create mode 100644 src/main/java/id/ac/ui/tutorial0/service/CourseService.java
 create mode 100644 src/main/java/id/ac/ui/tutorial0/service/CourseServiceImpl.java
 create mode 100644 src/main/resources/templates/courseList.html
 create mode 100644 src/main/resources/templates/createCourse.html

diff --git a/src/main/java/id/ac/ui/tutorial0/controller/CourseController.java b/src/main/java/id/ac/ui/tutorial0/controller/CourseController.java
new file mode 100644
index 0000000..6145e01
--- /dev/null
+++ b/src/main/java/id/ac/ui/tutorial0/controller/CourseController.java
@@ -0,0 +1,38 @@
+package id.ac.ui.tutorial0.controller;
+
+import id.ac.ui.tutorial0.model.Course;
+import id.ac.ui.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";
+    }
+}
diff --git a/src/main/java/id/ac/ui/tutorial0/model/Course.java b/src/main/java/id/ac/ui/tutorial0/model/Course.java
new file mode 100644
index 0000000..c5cbc2b
--- /dev/null
+++ b/src/main/java/id/ac/ui/tutorial0/model/Course.java
@@ -0,0 +1,12 @@
+package id.ac.ui.tutorial0.model;
+
+import lombok.Getter;
+import lombok.Setter;
+
+@Getter @Setter
+public class Course {
+    private String courseName;
+    private String courseID;
+    private boolean vacancyStatus = true;
+
+}
diff --git a/src/main/java/id/ac/ui/tutorial0/repository/CourseRepository.java b/src/main/java/id/ac/ui/tutorial0/repository/CourseRepository.java
new file mode 100644
index 0000000..2eb3142
--- /dev/null
+++ b/src/main/java/id/ac/ui/tutorial0/repository/CourseRepository.java
@@ -0,0 +1,23 @@
+package id.ac.ui.tutorial0.repository;
+
+import id.ac.ui.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();
+    }
+}
diff --git a/src/main/java/id/ac/ui/tutorial0/service/CourseService.java b/src/main/java/id/ac/ui/tutorial0/service/CourseService.java
new file mode 100644
index 0000000..9dd92ee
--- /dev/null
+++ b/src/main/java/id/ac/ui/tutorial0/service/CourseService.java
@@ -0,0 +1,11 @@
+package id.ac.ui.tutorial0.service;
+
+import id.ac.ui.tutorial0.model.Course;
+
+import java.util.List;
+
+public interface CourseService {
+
+    public Course create(Course course);
+    public List<Course> findAll();
+}
diff --git a/src/main/java/id/ac/ui/tutorial0/service/CourseServiceImpl.java b/src/main/java/id/ac/ui/tutorial0/service/CourseServiceImpl.java
new file mode 100644
index 0000000..ba51fc9
--- /dev/null
+++ b/src/main/java/id/ac/ui/tutorial0/service/CourseServiceImpl.java
@@ -0,0 +1,40 @@
+package id.ac.ui.tutorial0.service;
+
+import id.ac.ui.tutorial0.model.Course;
+import id.ac.ui.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;
+
+    public Course create(Course course) {
+        generateCourseID(course);
+        courseRepository.create(course);
+        return course;
+    }
+
+    private void generateCourseID(Course course) {
+        StringBuilder stringBuilder = new StringBuilder();
+        for (char letter: course.getCourseName().toCharArray()) {
+            stringBuilder.append(String.valueOf((int) letter));
+        }
+        String courseID = stringBuilder.toString();
+        course.setCourseID(courseID);
+    }
+
+    @Override
+    public List<Course> findAll() {
+        Iterator<Course> courseIterator = courseRepository.findAll();
+        List<Course> allCourses = new ArrayList<>();
+        courseIterator.forEachRemaining(allCourses::add);
+        return allCourses;
+    }
+}
diff --git a/src/main/resources/templates/courseList.html b/src/main/resources/templates/courseList.html
new file mode 100644
index 0000000..6116255
--- /dev/null
+++ b/src/main/resources/templates/courseList.html
@@ -0,0 +1,34 @@
+<!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">Course 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.courseName}"></td>
+        <td th:text="${course.courseID}"></td>
+        <td th:text="${course.vacancyStatus}"></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
diff --git a/src/main/resources/templates/createCourse.html b/src/main/resources/templates/createCourse.html
new file mode 100644
index 0000000..cdd301e
--- /dev/null
+++ b/src/main/resources/templates/createCourse.html
@@ -0,0 +1,27 @@
+<!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>
+<!--<div th:if="${error!=null}" class="alert alert-danger" role="alert">-->
+<!--    Duplicate student name-->
+<!--</div>-->
+<form th:action="@{/course/create}" th:object="${course}" method="post">
+    <div class="form-group">
+        <label for="nameInput">Course Name</label>
+        <input th:field="*{courseName}" type="text" class="form-control" id="nameInput" aria-describedby="nameHelp" placeholder="Enter course' name">
+        <small id="nameHelp" class="form-text text-muted">Please enter unique name.</small>
+    </div>
+<!--    <div class="form-group">-->
+<!--        <label for="addressTextarea">Address</label>-->
+<!--        <textarea th:field="*{address}" class="form-control" id="addressTextarea" rows="3"></textarea>-->
+<!--    </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
-- 
GitLab


From dfaf8cdc09d43620a0cc514d99022696f01a57ba Mon Sep 17 00:00:00 2001
From: "pramudya.wibisono" <pramudya.wibisono@ui.ac.id>
Date: Tue, 15 Feb 2022 19:19:36 +0700
Subject: [PATCH 3/3] Delete some comments

---
 src/main/resources/templates/createCourse.html | 7 -------
 1 file changed, 7 deletions(-)

diff --git a/src/main/resources/templates/createCourse.html b/src/main/resources/templates/createCourse.html
index cdd301e..67e0dd4 100644
--- a/src/main/resources/templates/createCourse.html
+++ b/src/main/resources/templates/createCourse.html
@@ -7,19 +7,12 @@
 </head>
 <body>
 <h3>Create new Course</h3>
-<!--<div th:if="${error!=null}" class="alert alert-danger" role="alert">-->
-<!--    Duplicate student name-->
-<!--</div>-->
 <form th:action="@{/course/create}" th:object="${course}" method="post">
     <div class="form-group">
         <label for="nameInput">Course Name</label>
         <input th:field="*{courseName}" type="text" class="form-control" id="nameInput" aria-describedby="nameHelp" placeholder="Enter course' name">
         <small id="nameHelp" class="form-text text-muted">Please enter unique name.</small>
     </div>
-<!--    <div class="form-group">-->
-<!--        <label for="addressTextarea">Address</label>-->
-<!--        <textarea th:field="*{address}" class="form-control" id="addressTextarea" rows="3"></textarea>-->
-<!--    </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>
-- 
GitLab