|
| 1 | +package com.youdemy.controller; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.net.URI; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.sql.SQLException; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.Collection; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Objects; |
| 13 | +import java.util.Optional; |
| 14 | + |
| 15 | +import org.hibernate.engine.jdbc.BlobProxy; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.http.HttpStatus; |
| 18 | +import org.springframework.http.ResponseEntity; |
| 19 | +import org.springframework.ui.Model; |
| 20 | +import org.springframework.util.ResourceUtils; |
| 21 | +import org.springframework.web.bind.annotation.GetMapping; |
| 22 | +import org.springframework.web.bind.annotation.PathVariable; |
| 23 | +import org.springframework.web.bind.annotation.PostMapping; |
| 24 | +import org.springframework.web.bind.annotation.PutMapping; |
| 25 | +import org.springframework.web.bind.annotation.RequestBody; |
| 26 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 27 | +import org.springframework.web.bind.annotation.RequestParam; |
| 28 | +import org.springframework.web.bind.annotation.ResponseStatus; |
| 29 | +import org.springframework.web.bind.annotation.RestController; |
| 30 | +import org.springframework.web.multipart.MultipartFile; |
| 31 | +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; |
| 32 | + |
| 33 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 34 | +import com.youdemy.model.Course; |
| 35 | +import com.youdemy.model.Lesson; |
| 36 | +import com.youdemy.model.User; |
| 37 | +import com.youdemy.service.CourseService; |
| 38 | +import com.youdemy.service.UserService; |
| 39 | + |
| 40 | + |
| 41 | +@RestController |
| 42 | +@RequestMapping("/api/courses") |
| 43 | +public class CourseRestController { |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private CourseService courseService; |
| 47 | + |
| 48 | + @Autowired |
| 49 | + UserService userService; |
| 50 | + |
| 51 | + @GetMapping("/") |
| 52 | + public Collection<Course> getCourses() { |
| 53 | + return courseService.findAll(); |
| 54 | + } |
| 55 | + |
| 56 | + @GetMapping("/{id}") |
| 57 | + public ResponseEntity<Course> getCourse(@PathVariable long id){ |
| 58 | + Optional<Course> op = courseService.findById(id); |
| 59 | + if (op.isPresent()) { |
| 60 | + Course course = op.get(); |
| 61 | + return new ResponseEntity<>(course, HttpStatus.OK); |
| 62 | + } else { |
| 63 | + return new ResponseEntity<>(HttpStatus.NOT_FOUND); |
| 64 | + } |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | +// @PostMapping("/") |
| 69 | +// public ResponseEntity<Course> createcourse(@RequestBody Course course) { |
| 70 | +// |
| 71 | +// courseService.save(course); |
| 72 | +// |
| 73 | +// URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}").buildAndExpand(course.getId()).toUri(); |
| 74 | +// |
| 75 | +// return ResponseEntity.created(location).body(course); |
| 76 | +// } |
| 77 | + |
| 78 | + @PostMapping("/") |
| 79 | + public String postNewCourse(@RequestParam("title") String title, @RequestParam("thumbnail") String image, |
| 80 | + @RequestParam("description") String description, @RequestParam("price") int price, |
| 81 | + @RequestParam("tags") List<String> tags, @RequestBody("lessons") List<Lesson> lessons, |
| 82 | + Model model) throws IOException { |
| 83 | + |
| 84 | + Course course = new Course(); |
| 85 | + User author = userService.findByFirstName(Objects.requireNonNull(model.getAttribute("userName")).toString()); |
| 86 | + |
| 87 | + List<Lesson> lessonList = new ArrayList<>(Arrays.asList(new ObjectMapper().readValues(lessons, Lesson[].class))); |
| 88 | + lessonList.forEach(lesson -> { |
| 89 | + lesson.setAuthor(author); |
| 90 | + lesson.setCourse(course); |
| 91 | + }); |
| 92 | + |
| 93 | + course.setAuthor(author); |
| 94 | + course.setThumbnail(loadRandomImage()); |
| 95 | + course.setTitle(title); |
| 96 | + course.setDescription(description); |
| 97 | + course.setPrice(price); |
| 98 | + course.setTags(tags); |
| 99 | + course.setLessons(lessonList); |
| 100 | + |
| 101 | + courseService.save(course); |
| 102 | + return "redirect:/courses"; |
| 103 | + } |
| 104 | + |
| 105 | + public byte[] loadRandomImage() throws IOException { |
| 106 | + int randomImgNum = (int) Math.floor(Math.random() * 9) + 1; |
| 107 | + File image = ResourceUtils.getFile("classpath:./fakeImages/" + randomImgNum + ".jpg"); |
| 108 | + |
| 109 | + return Files.readAllBytes(image.toPath()); |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +} |
0 commit comments