|
| 1 | +package com.youdemy.controller.api; |
| 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 com.youdemy.controller.BasicAttributes; |
| 16 | +import org.hibernate.engine.jdbc.BlobProxy; |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; |
| 18 | +import org.springframework.data.domain.Page; |
| 19 | +import org.springframework.data.domain.PageRequest; |
| 20 | +import org.springframework.http.HttpStatus; |
| 21 | +import org.springframework.http.ResponseEntity; |
| 22 | +import org.springframework.ui.Model; |
| 23 | +import org.springframework.util.ResourceUtils; |
| 24 | +import org.springframework.web.bind.annotation.*; |
| 25 | +import org.springframework.web.multipart.MultipartFile; |
| 26 | +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; |
| 27 | + |
| 28 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 29 | +import com.youdemy.model.Course; |
| 30 | +import com.youdemy.model.Lesson; |
| 31 | +import com.youdemy.model.User; |
| 32 | +import com.youdemy.service.CourseService; |
| 33 | +import com.youdemy.service.UserService; |
| 34 | + |
| 35 | +import javax.servlet.http.HttpServletRequest; |
| 36 | + |
| 37 | + |
| 38 | +@RestController |
| 39 | +@RequestMapping("/api/courses") |
| 40 | +public class CourseRestController { |
| 41 | + |
| 42 | + @Autowired |
| 43 | + private CourseService courseService; |
| 44 | + |
| 45 | + @Autowired |
| 46 | + private UserService userService; |
| 47 | + |
| 48 | + @ModelAttribute |
| 49 | + public void addAttributes(Model model, HttpServletRequest request) { |
| 50 | + BasicAttributes.addAttributes(model, request, userService); |
| 51 | + } |
| 52 | + |
| 53 | + @GetMapping("/{id}") |
| 54 | + public Course getById(@PathVariable long id) { |
| 55 | + Optional<Course> course = courseService.findById(id); |
| 56 | + |
| 57 | + return course.orElse(null); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) |
| 62 | + public ResponseEntity delete(@PathVariable long id) { |
| 63 | + courseService.delete(id); |
| 64 | + |
| 65 | + return ResponseEntity.noContent().build(); |
| 66 | + } |
| 67 | + |
| 68 | + @RequestMapping(value = "/page", method = RequestMethod.GET) |
| 69 | + @ResponseBody |
| 70 | + public Page<Course> getCoursesInPage(@RequestParam Optional<Integer> page, |
| 71 | + @RequestParam Optional<String> search) { |
| 72 | + System.out.println(page.orElse(0)); |
| 73 | + |
| 74 | + return courseService.findByTitle(search.orElse(""), |
| 75 | + PageRequest.of(page.orElse(0), 6)); |
| 76 | + } |
| 77 | + |
| 78 | + @PostMapping("/new") |
| 79 | + public Course create(@RequestAttribute("title") String title, @RequestAttribute("image") MultipartFile image, |
| 80 | + @RequestAttribute("description") String description, @RequestAttribute("price") int price, |
| 81 | + @RequestAttribute("tags") String tags, @RequestAttribute("lessons") String lessons, |
| 82 | + Model model) throws IOException { |
| 83 | + Course course = new Course(); |
| 84 | + User author = userService.findByFirstName(Objects.requireNonNull(model.getAttribute("userName")).toString()); |
| 85 | + |
| 86 | + List<Lesson> lessonList = new ArrayList<>(Arrays.asList(new ObjectMapper().readValue(lessons, Lesson[].class))); |
| 87 | + lessonList.forEach(lesson -> { |
| 88 | + lesson.setAuthor(author); |
| 89 | + lesson.setCourse(course); |
| 90 | + }); |
| 91 | + |
| 92 | + course.setAuthor(author); |
| 93 | + course.setThumbnail(image.getBytes()); |
| 94 | + course.setTitle(title); |
| 95 | + course.setDescription(description); |
| 96 | + course.setPrice(price); |
| 97 | + course.setTags(new ArrayList(Arrays.asList(new ObjectMapper().readValue(tags, String[].class)))); |
| 98 | + course.setLessons(lessonList); |
| 99 | + |
| 100 | + courseService.save(course); |
| 101 | + |
| 102 | + return courseService.findById(course.getId()).get(); |
| 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