Fakultas Ilmu Komputer UI

Skip to content
Snippets Groups Projects
Commit 128010d1 authored by Ardanisa Rachma's avatar Ardanisa Rachma
Browse files

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

Revert "create class MockStudentServiceImpl"

See merge request !4
parents 8ae1ecfa a7c72a26
No related branches found
No related tags found
1 merge request!4Revert "create class MockStudentServiceImpl"
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";
}
}
package id.ac.ui.tutorial0.model;
import lombok.Getter;
import lombok.Setter;
@Getter @Setter
public class Course {
private String courseId;
private String courseName;
private boolean vacancyStatus;
}
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> courseInMemory = new ArrayList<>();
public Course create(Course course) {
courseInMemory.add(course);
return course;
}
public Iterator<Course> findAll() {
return courseInMemory.iterator();
}
}
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();
}
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;
@Override
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 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">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>
<form th:action="@{/course/create}" th:object="${course}" method="post">
<div class="form-group">
<label for="idInput">Course Id</label>
<input th:field="*{courseId}" type="text" class="form-control" id="idInput" aria-describedby="idHelp" placeholder="Enter course' id">
</div>
<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">
</div>
<div class="form-group">
<label for="vacancyStatus">Vacancy Status</label>
<input th:field="*{vacancyStatus}" class="form-control" id="vacancyStatus" rows="3"></input>
</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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment