---
# System prepended metadata

title: 20230516 lab2

---

# 20230516 lab2
# CourseBeanRestController
```
package com.ctbcbank.demo.lab2.controller.rest;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.ctbcbank.demo.lab2.bean.CourseBean;
import com.ctbcbank.demo.lab2.repository.CourseBeanRepository;

import lombok.extern.slf4j.Slf4j;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/api/course")
public class CourseBeanRestController {
	@Autowired
	private CourseBeanRepository repository;

//	取得所有課程
	@RequestMapping(value = "/all", method = RequestMethod.GET)
	public List<CourseBean> getAllCourses() {
		List<CourseBean> courses = new ArrayList<>();
		repository.findAll().forEach(c -> courses.add(c));
		return courses;
	}

//	取得指定id
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public CourseBean getCourseBeanById(@PathVariable long id) {
		CourseBean courseBean = repository.findById(id).get();
		return courseBean;
	}

//	刪除指定id
	@DeleteMapping(value = "/{id}")
	public void removeCourseBean(@PathVariable long id) {
		repository.deleteById(id);
	}

//	新增課程 no
	@RequestMapping(value = "/all", method = RequestMethod.POST)
	public void createCourse(@RequestBody CourseBean bean) {
		repository.save(bean);
	}
	
//	修改課程 no
	@RequestMapping(value = "/{id}",method=RequestMethod.PUT)
	public void modifyCourse(@PathVariable CourseBean bean) {
		List<CourseBean> courses = new ArrayList<>();
		repository.findAll().forEach(c -> courses.add(c));
		for (CourseBean course : courses) {
			if(course.getId()!=bean.getId()) {
				course.setName(bean.getName());
				course.setPrice(bean.getPrice());
				repository.save(bean);
			}
			else {
				log.info(bean.getName(),"課程已存在");
			}
		}
	}
}

```
# CourseCategoryBeanRestController
```
package com.ctbcbank.demo.lab2.controller.rest;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.ctbcbank.demo.lab2.bean.CourseBean;
import com.ctbcbank.demo.lab2.bean.CourseCategoryBean;
import com.ctbcbank.demo.lab2.repository.CourseCategoryBeanRepository;

import lombok.extern.slf4j.Slf4j;

@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/api/courseCategory")
public class CourseCategoryBeanRestController {
	@Autowired
	private CourseCategoryBeanRepository repository;

//	取得所有類別 
	@RequestMapping(value = "/all", method = RequestMethod.GET)
	public List<CourseCategoryBean> getAllCategory() {
		List<CourseCategoryBean> categories = new ArrayList<>();
		repository.findAll().forEach(c -> categories.add(c));
		return categories;
	}

//	取得指定id
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public CourseCategoryBean getCourseCategoryBeanById(@PathVariable long id) {
		CourseCategoryBean courseCategoryBean = repository.findById(id).get();
		return courseCategoryBean;
	}

//	刪除指定id
	@DeleteMapping(value = "/{id}")
	public void removeCourseCategoryBean(@PathVariable long id) {
		repository.deleteById(id);
	}

//	新增課程 no
	@RequestMapping(value = "/all", method = RequestMethod.POST)
//	@ResponseStatus(HttpStatus.CREATED)
	public void createCourseCategory(@RequestBody CourseCategoryBean bean) {
		repository.save(bean);
	}
	
//	修改課程
//	@RequestMapping(value = "/{id}",method=RequestMethod.PUT)
//	@ResponseStatus(HttpStatus.CREATED)
//	public void modifyCourseCategory(@PathVariable CourseCategoryBean bean) {
//		List<CourseBean> courses = new ArrayList<>();
//		repository.findAll().forEach(c -> courses.add(c));
//		for (CourseBean course : courses) {
//			if(course.getId()!=bean.getId()) {
//				course.setName(bean.getName());
//				course.setPrice(bean.getPrice());
//				repository.save(bean);
//			}
//			else {
//				log.info(bean.getName(),"課程已存在");
//			}
//		}
//	}
}

```