Fakultas Ilmu Komputer UI
Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
samuel sharon sidabutar
tutorial-0
Commits
2c5b4fe2
Commit
2c5b4fe2
authored
Feb 13, 2022
by
Samuel Sharon
Browse files
Implement Course Page
parent
63ac8cc7
Changes
7
Hide whitespace changes
Inline
Side-by-side
src/main/java/id/ac/ui/cs/advprog/tutorial0/controller/CourseController.java
0 → 100644
View file @
2c5b4fe2
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"
;
}
}
src/main/java/id/ac/ui/cs/advprog/tutorial0/model/Course.java
0 → 100644
View file @
2c5b4fe2
package
id.ac.ui.cs.advprog.tutorial0.model
;
import
lombok.Getter
;
import
lombok.Setter
;
@Getter
@Setter
public
class
Course
{
private
String
id
;
private
String
name
;
private
Boolean
vacancyStatus
=
true
;
}
src/main/java/id/ac/ui/cs/advprog/tutorial0/repository/CourseRepository.java
0 → 100644
View file @
2c5b4fe2
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
();
}
}
src/main/java/id/ac/ui/cs/advprog/tutorial0/service/CourseService.java
0 → 100644
View file @
2c5b4fe2
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
();
}
\ No newline at end of file
src/main/java/id/ac/ui/cs/advprog/tutorial0/service/CourseServiceImpl.java
0 → 100644
View file @
2c5b4fe2
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
.
setId
(
id
);
}
@Override
public
List
<
Course
>
findAll
()
{
Iterator
<
Course
>
courseIterator
=
courseRepository
.
findAll
();
List
<
Course
>
allCourses
=
new
ArrayList
<>();
courseIterator
.
forEachRemaining
(
allCourses:
:
add
);
return
allCourses
;
}
}
src/main/resources/templates/courseList.html
0 → 100644
View file @
2c5b4fe2
<!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>
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"
>
ID
</th>
<th
scope=
"col"
>
Vacancy
</th>
</tr>
</thead>
<tbody
th:each=
"course: ${courses}"
>
<tr>
<td
th:text=
"${course.name}"
></td>
<td
th:text=
"${course.id}"
></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
src/main/resources/templates/createCourse.html
0 → 100644
View file @
2c5b4fe2
<!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"
>
Name
</label>
<input
th:field=
"*{name}"
type=
"text"
class=
"form-control"
id=
"nameInput"
aria-describedby=
"nameHelp"
placeholder=
"Enter course name"
>
</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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment