Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лаб_1 Головков И.Е. 12002108

.pdf
Скачиваний:
0
Добавлен:
26.06.2024
Размер:
1.73 Mб
Скачать

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.*;

@Controller

@AllArgsConstructor

@RequestMapping("/sportline/products") public class ProductController {

private static final String REDIRECT_LIST = "redirect:/sportline/products"; private static final String REDIRECT_ITEM = "redirect:/sportline/products/{id}";

private ProductService productService; private BrandService brandService;

@PostMapping("")

public String postProduct(@ModelAttribute("product") Product product) { productService.create(product);

return REDIRECT_LIST;

}

@PutMapping("/{id}")

public String putProduct(@PathVariable("id") long id, @ModelAttribute("product") Product product) {

productService.updateById(product, id); return REDIRECT_ITEM;

}

@DeleteMapping("/{id}")

public String deleteProduct(@PathVariable("id") long id) {

productService.deleteById(id);

31

return REDIRECT_LIST;

}

@GetMapping("")

public String getProducts(Model model, @ModelAttribute("product") Product product,

@RequestParam(name = "pageNum", required = false, defaultValue = "0") int pageNum,

@RequestParam(name = "pageSize", required = false, defaultValue = "4") int pageSize) {

model.addAttribute("productsPage", productService.findAllByPage(pageNum, pageSize));

model.addAttribute("brandsList", brandService.findAll()); model.addAttribute("statusesArray", Status.values()); model.addAttribute("productsList", productService.findAllByPage(pageNum,

pageSize).getContent());

return "sportline/product/list";

}

@GetMapping("/{id}")

public String getProduct(Model model, @PathVariable("id") long id) {

model.addAttribute("productItem", productService.getById(id)); model.addAttribute("brandsList", brandService.findAll()); model.addAttribute("statusesArray", Status.values());

return "sportline/product/item";

}

}

32

package com.sportline.controller;

import com.sportline.model.entity.SpecialOffer; import com.sportline.service.SpecialOfferService; import lombok.AllArgsConstructor;

import org.springframework.stereotype.Controller; import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.*;

@Controller

@AllArgsConstructor

@RequestMapping("/sportline/offers") public class SpecialOfferController {

private static final String REDIRECT_LIST = "redirect:/sportline/offers"; private static final String REDIRECT_ITEM = "redirect:/sportline/offers/{id}";

private SpecialOfferService specialOfferService;

@PostMapping("")

public String postSpecialOffer(@ModelAttribute("offer") SpecialOffer specialOffer) {

specialOfferService.create(specialOffer); return REDIRECT_LIST;

}

@PutMapping("/{id}")

public String putSpecialOffer(@PathVariable("id") long id, @ModelAttribute("offer") SpecialOffer specialOffer) {

specialOfferService.updateById(specialOffer, id);

33

return REDIRECT_ITEM;

}

@DeleteMapping("/{id}")

public String deleteSpecialOffer(@PathVariable("id") long id) { specialOfferService.deleteById(id);

return REDIRECT_LIST;

}

@GetMapping("")

public String getSpecialOffer(Model model,

@ModelAttribute("offer") SpecialOffer specialOffer, @RequestParam(name = "pageNum", required = false,

defaultValue = "0") int pageNum,

@RequestParam(name = "pageSize", required = false, defaultValue = "4") int pageSize) {

model.addAttribute("offersPage", specialOfferService.findAllByPage(pageNum, pageSize));

model.addAttribute("offersList", specialOfferService.findAllByPage(pageNum, pageSize).getContent());

return "sportline/offer/list";

}

@GetMapping("/{id}")

public String getSpecialOffer(Model model, @PathVariable("id") long id) {

model.addAttribute("offerItem", specialOfferService.getById(id)); return "sportline/offer/item";

}

}

34

Приложение Б

Сервисы

package com.sportline.service.impl;

import com.sportline.exc.CustomNotFoundException; import com.sportline.model.entity.BlogPost;

import com.sportline.repository.BlogRepository; import com.sportline.service.BlogService; import lombok.AllArgsConstructor;

import org.springframework.data.domain.Page;

import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime; import java.util.List;

@Service

@AllArgsConstructor @Transactional(readOnly = true)

public class BlogServiceImpl implements BlogService {

private final BlogRepository blogRepository;

@Transactional

@Override

public void create(BlogPost blogPost) {

blogPost.setPublicationDate(LocalDateTime.now());

35

blogRepository.save(blogPost);

}

@Override

public List<BlogPost> findAll() { return blogRepository.findAll();

}

@Override

public Page<BlogPost> findAllByPage(int pageNum, int pageSize) { return blogRepository.findAll(

PageRequest.of(pageNum, pageSize,

Sort.by(Sort.Direction.DESC, "publicationDate")

));

}

@Override

public BlogPost getById(Long id) { return blogRepository

.findById(id)

.orElseThrow(() -> new CustomNotFoundException("Запись в блоге",

id));

}

@Transactional

@Override

public void updateById(BlogPost blogPost, long id) { BlogPost blogPostBefore = getById(id); blogPostBefore.setHeading(blogPost.getHeading());

blogPostBefore.setContent(blogPost.getContent());

36

blogRepository.save(blogPostBefore);

}

@Transactional

@Override

public void deleteById(Long id) { blogRepository.deleteById(id);

}

}

package com.sportline.service.impl;

import com.sportline.exc.CustomNotFoundException; import com.sportline.model.entity.Brand;

import com.sportline.repository.BrandRepository; import com.sportline.service.BrandService; import lombok.AllArgsConstructor;

import org.springframework.data.domain.Page;

import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service

@AllArgsConstructor @Transactional(readOnly = true)

public class BrandServiceImpl implements BrandService {

37

private final BrandRepository brandRepository;

@Transactional

@Override

public void create(Brand brand) {

brandRepository.save(brand);

}

@Override

public List<Brand> findAll() { return brandRepository.findAll();

}

@Override

public Page<Brand> findAllByPage(int pageNum, int pageSize) { return brandRepository.findAll(PageRequest.of(pageNum, pageSize));

}

@Override

public Brand getById(Long id) { return brandRepository

.findByIdWithItems(id)

.orElseThrow(() -> new CustomNotFoundException("Бренд", id));

}

@Transactional

@Override

public void updateById(Brand brand, long id) { brand.setId(id); brandRepository.save(brand);

}

38

@Transactional

@Override

public void deleteById(Long id) { brandRepository.deleteById(id);

}

}

package com.sportline.service.impl;

import com.sportline.exc.CustomNotFoundException; import com.sportline.model.entity.Product;

import com.sportline.repository.ProductRepository; import com.sportline.service.ProductService; import lombok.AllArgsConstructor;

import org.springframework.data.domain.Page;

import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service

@AllArgsConstructor @Transactional(readOnly = true)

public class ProductServiceImpl implements ProductService {

private final ProductRepository productRepository;

@Transactional

39

@Override

public void create(Product product) {

productRepository.save(product);

}

@Override

public List<Product> findAll() { return productRepository.findAll();

}

@Override

public Page<Product> findAllByPage(int pageNum, int pageSize) {

return productRepository.findAll(PageRequest.of(pageNum, pageSize));

}

@Override

public Product getById(Long id) { return productRepository

.findById(id)

.orElseThrow(() -> new CustomNotFoundException("Товар", id));

}

@Transactional

@Override

public void updateById(Product product, long id) { product.setId(id); productRepository.save(product);

}

@Transactional

40