Fakultas Ilmu Komputer UI

Commit 71d8632d authored by ZETA PRAWIRA SYAH's avatar ZETA PRAWIRA SYAH
Browse files

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

Finished Course

See merge request !3
parents 7ada9d25 facbe654
package id.ac.ui.cs.advprog.tutorial0.controller;
import id.ac.ui.cs.advprog.tutorial0.exception.DuplicateCourseNameException;
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> allCourse = service.findAll();
model.addAttribute("courses", allCourse);
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) {
try {
service.create(course);
} catch (DuplicateCourseNameException e) {
model.addAttribute("error", e);
model.addAttribute("course", course);
return "createCourse";
}
return "redirect:list";
}
}
\ No newline at end of file
package id.ac.ui.cs.advprog.tutorial0.exception;
public class DuplicateCourseNameException extends RuntimeException {
public DuplicateCourseNameException(String courseName) {
super(String.format("The course name %s is a duplicate!",courseName));
}
}
\ No newline at end of file
package id.ac.ui.cs.advprog.tutorial0.model;
import lombok.Getter;
import lombok.Setter;
@Getter @Setter
public class Course {
private String courseId;
private String courseName;
private boolean vacancyStatus = 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> courseInMemory = new ArrayList<>();
public Course create(Course course) {
courseInMemory.add(course);
return course;
}
public Iterator<Course> findAll() {
return courseInMemory.iterator();
}
}
package id.ac.ui.cs.advprog.tutorial0.repository;
import id.ac.ui.cs.advprog.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<>();
......
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.exception.DuplicateCourseNameException;
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 CourseServicelmpl implements CourseService {
@Autowired
private CourseRepository courseRepository;
@Override
public Course create(Course course) {
validateName(course);
generateNPM(course);
courseRepository.create(course);
return course;
}
private void validateName(Course course) {
List<Course> allCourse = findAll();
for(Course dbCourse: allCourse) {
if(dbCourse.getCourseName().equals(course.getCourseName())) {
throw new DuplicateCourseNameException(course.getCourseName());
}
}
}
private void generateNPM(Course course) {
StringBuilder stringBuilder = new StringBuilder();
for (char letter: course.getCourseName().toCharArray()) {
stringBuilder.append(String.valueOf((int)letter));
}
String npm = stringBuilder.toString();
course.setCourseId(npm);
}
@Override
public List<Course> findAll() {
Iterator<Course> courseIterator = courseRepository.findAll();
List<Course> allCourse = new ArrayList<>();
courseIterator.forEachRemaining(allCourse::add);
return allCourse;
}
}
\ No newline at end of file
<!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 ID</th>
<th scope="col">Course Name</th>
<th scope="col">Vacancy Status</th>
</tr>
</thead>
<tbody th:each="course: ${courses}">
<tr>
<td th:text="${course.courseId}"></td>
<td th:text="${course.courseName}"></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
<!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 course 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="vacancyCheck">Vacancy</label>
<input type="checkbox" th:field="*{vacancyStatus}" id="vacancyCheck">
</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