Subversion Repositories SmartDukaan

Rev

Rev 21440 | Go to most recent revision | Details | 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.time.LocalDateTime;
4
import java.util.ArrayList;
5
import java.util.HashSet;
6
import java.util.List;
7
import java.util.Set;
8
 
9
import javax.servlet.http.HttpServletRequest;
10
 
11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.http.HttpStatus;
15
import org.springframework.http.ResponseEntity;
16
import org.springframework.stereotype.Controller;
17
import org.springframework.web.bind.annotation.RequestMapping;
18
import org.springframework.web.bind.annotation.RequestMethod;
19
import org.springframework.web.bind.annotation.RequestParam;
20
 
21
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
22
import com.spice.profitmandi.common.model.ProfitMandiConstants;
23
import com.spice.profitmandi.dao.entity.Brand;
24
import com.spice.profitmandi.dao.repository.BrandRepository;
25
import com.spice.profitmandi.web.model.ProfitMandiResponse;
26
import com.spice.profitmandi.web.model.Response;
27
import com.spice.profitmandi.web.model.ResponseStatus;
28
import com.spice.profitmandi.web.req.Category;
29
 
30
@Controller
31
public class BrandController {
32
 
33
	private static final Logger LOGGER=LoggerFactory.getLogger(BrandController.class);
34
 
35
	@Autowired
36
	BrandRepository brandRepository;
37
 
38
	@RequestMapping(value = ProfitMandiConstants.URL_BRAND_ALL, method=RequestMethod.GET)
39
	public ResponseEntity<?> getAll(HttpServletRequest request){
40
		LOGGER.info("requested url : "+request.getRequestURL().toString());
41
		List<Brand> brands = brandRepository.selectAll();
42
		List<Category> categories = new ArrayList<>();
43
		for(Brand brand : brands){
44
			Category category = new Category();
45
			category.setId(brand.getCategoryId());
46
			if(!categories.contains(category)){
47
				switch(brand.getCategoryId()){
48
					case 3:{
49
						category.setName("Mobiles");
50
						break;
51
					}case 5:{
52
						category.setName("Tablets");
53
						break;
54
					}case 6:{
55
						category.setName("Accessories");
56
						break;
57
					}
58
				}
59
				com.spice.profitmandi.web.req.Brand brandModel = new com.spice.profitmandi.web.req.Brand();
60
				brandModel.setId(brand.getId());
61
				brandModel.setName(brand.getName());
62
				Set<com.spice.profitmandi.web.req.Brand> brandsModels = new HashSet<>();
63
				brandsModels.add(brandModel);
64
				category.setBrands(brandsModels);
65
				categories.add(category);
66
			}else{
67
				Category foundCategory = categories.get(categories.indexOf(category));
68
				com.spice.profitmandi.web.req.Brand brandModel = new com.spice.profitmandi.web.req.Brand();
69
				brandModel.setId(brand.getId());
70
				brandModel.setName(brand.getName());
71
				foundCategory.getBrands().add(brandModel);
72
			}
73
		}
74
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, categories);
75
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
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 {
82
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, brandRepository.selectById(id));
83
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
84
		}catch (ProfitMandiBusinessException pmbe) {
85
			LOGGER.error("ProfitMandi error: ", pmbe);
86
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
87
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
88
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
89
		}
90
	}
91
 
92
 
93
}