Subversion Repositories SmartDukaan

Rev

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