Subversion Repositories SmartDukaan

Rev

Rev 23568 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21431 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.util.ArrayList;
4
import java.util.HashSet;
5
import java.util.List;
6
import java.util.Set;
7
 
8
import javax.servlet.http.HttpServletRequest;
9
 
23568 govind 10
import org.apache.logging.log4j.Logger;
11
import org.apache.logging.log4j.LogManager;
21431 ashik.ali 12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.http.ResponseEntity;
14
import org.springframework.stereotype.Controller;
21702 ashik.ali 15
import org.springframework.transaction.annotation.Transactional;
21431 ashik.ali 16
import org.springframework.web.bind.annotation.RequestMapping;
17
import org.springframework.web.bind.annotation.RequestMethod;
18
import org.springframework.web.bind.annotation.RequestParam;
19
 
20
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
21
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21740 ashik.ali 22
import com.spice.profitmandi.common.web.util.ResponseSender;
21735 ashik.ali 23
import com.spice.profitmandi.dao.entity.dtr.Brand;
24
import com.spice.profitmandi.dao.repository.dtr.BrandRepository;
21431 ashik.ali 25
import com.spice.profitmandi.web.req.Category;
26
 
27
@Controller
35434 amit 28
@Transactional(readOnly = true, rollbackFor = Throwable.class)
21431 ashik.ali 29
public class BrandController {
21448 ashik.ali 30
 
31
	@Autowired
22931 ashik.ali 32
	private ResponseSender<?> responseSender;
21431 ashik.ali 33
 
23568 govind 34
	private static final Logger LOGGER=LogManager.getLogger(BrandController.class);
21431 ashik.ali 35
 
36
	@Autowired
22931 ashik.ali 37
	private BrandRepository brandRepository;
21431 ashik.ali 38
 
39
	@RequestMapping(value = ProfitMandiConstants.URL_BRAND_ALL, method=RequestMethod.GET)
21496 ashik.ali 40
	public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize){
21431 ashik.ali 41
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21496 ashik.ali 42
		List<Brand> brands = brandRepository.selectAll(pageNumber, pageSize);
21431 ashik.ali 43
		List<Category> categories = new ArrayList<>();
44
		for(Brand brand : brands){
45
			Category category = new Category();
46
			category.setId(brand.getCategoryId());
47
			if(!categories.contains(category)){
48
				switch(brand.getCategoryId()){
49
					case 3:{
50
						category.setName("Mobiles");
51
						break;
52
					}case 5:{
53
						category.setName("Tablets");
54
						break;
55
					}case 6:{
56
						category.setName("Accessories");
57
						break;
58
					}
59
				}
60
				com.spice.profitmandi.web.req.Brand brandModel = new com.spice.profitmandi.web.req.Brand();
61
				brandModel.setId(brand.getId());
62
				brandModel.setName(brand.getName());
63
				Set<com.spice.profitmandi.web.req.Brand> brandsModels = new HashSet<>();
64
				brandsModels.add(brandModel);
65
				category.setBrands(brandsModels);
66
				categories.add(category);
67
			}else{
68
				Category foundCategory = categories.get(categories.indexOf(category));
69
				com.spice.profitmandi.web.req.Brand brandModel = new com.spice.profitmandi.web.req.Brand();
70
				brandModel.setId(brand.getId());
71
				brandModel.setName(brand.getName());
72
				foundCategory.getBrands().add(brandModel);
73
			}
74
		}
21448 ashik.ali 75
		return responseSender.ok(categories);
21431 ashik.ali 76
	}
77
 
78
	@RequestMapping(value = ProfitMandiConstants.URL_BRAND_ID, method=RequestMethod.GET)
79
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
80
		LOGGER.info("requested url : "+request.getRequestURL().toString());
81
		try {
21448 ashik.ali 82
			return responseSender.ok(brandRepository.selectById(id));
21440 ashik.ali 83
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
84
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
21448 ashik.ali 85
			return responseSender.badRequest(profitMandiBusinessException);
21431 ashik.ali 86
		}
87
	}
88
 
89
 
90
}