Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
22860 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.util.List;
4
 
5
import javax.servlet.http.HttpServletRequest;
6
 
7
import org.slf4j.Logger;
8
import org.slf4j.LoggerFactory;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.stereotype.Controller;
11
import org.springframework.transaction.annotation.Transactional;
12
import org.springframework.ui.Model;
13
import org.springframework.web.bind.annotation.RequestBody;
14
import org.springframework.web.bind.annotation.RequestMapping;
15
import org.springframework.web.bind.annotation.RequestMethod;
16
import org.springframework.web.bind.annotation.RequestParam;
17
 
18
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
19
import com.spice.profitmandi.common.model.CreateSchemeRequest;
20
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21
import com.spice.profitmandi.dao.entity.catalog.Scheme;
22
import com.spice.profitmandi.dao.repository.catalog.SchemeRepository;
23
import com.spice.profitmandi.service.scheme.SchemeService;
24
import com.spice.profitmandi.web.model.LoginDetails;
25
import com.spice.profitmandi.web.util.CookiesProcessor;
26
 
27
@Controller
28
@Transactional(rollbackFor=Throwable.class)
29
public class SchemeController {
30
 
31
	private static final Logger LOGGER = LoggerFactory.getLogger(SchemeController.class);
32
 
33
	@Autowired
22927 ashik.ali 34
	private SchemeService schemeService;
22860 ashik.ali 35
 
36
	@Autowired
22927 ashik.ali 37
	private SchemeRepository schemeRepository;
22860 ashik.ali 38
 
39
	@Autowired
22927 ashik.ali 40
	private CookiesProcessor cookiesProcessor;
22860 ashik.ali 41
 
42
	@RequestMapping(value = "/createScheme", method = RequestMethod.GET)
22927 ashik.ali 43
	public String createScheme(HttpServletRequest request, Model model){
22860 ashik.ali 44
		return "create-scheme";
45
	}
46
 
47
	@RequestMapping(value = "/createScheme", method = RequestMethod.POST)
22927 ashik.ali 48
	public String createScheme(HttpServletRequest request, @RequestBody CreateSchemeRequest createSchemeRequest, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)  throws ProfitMandiBusinessException{
49
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
22860 ashik.ali 50
		LOGGER.info("CreateSchemeRequest {}", createSchemeRequest);
22927 ashik.ali 51
		schemeService.saveScheme(loginDetails.getFofoId(), createSchemeRequest);
52
		LOGGER.info("Scheme saved successfully");
53
 
54
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
55
		long count = schemeRepository.selectCount(offset, limit);
56
		model.addAttribute("schemes", schemes);
57
		model.addAttribute("start", offset + 1);
58
		model.addAttribute("size", count);
59
		if (schemes.size() < limit){
60
			model.addAttribute("end", offset + schemes.size());
22860 ashik.ali 61
		}
22927 ashik.ali 62
		else{
63
			model.addAttribute("end", offset + limit);
64
		}
65
		return "schemes";
66
 
22860 ashik.ali 67
	}
68
 
69
	@RequestMapping(value = "/getSchemes", method = RequestMethod.GET)
22927 ashik.ali 70
	public String getSchemes(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model){
22860 ashik.ali 71
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
72
		long count = schemeRepository.selectCount(offset, limit);
73
		model.addAttribute("schemes", schemes);
74
		model.addAttribute("start", offset + 1);
75
		model.addAttribute("size", count);
76
		if (schemes.size() < limit){
77
			model.addAttribute("end", offset + schemes.size());
78
		}
79
		else{
80
			model.addAttribute("end", offset + limit);
81
		}
82
		return "schemes";
83
	}
84
 
85
	@RequestMapping(value = "/getPaginatedSchemes", method = RequestMethod.GET)
22927 ashik.ali 86
	public String getPaginatedSchemes(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model){
22860 ashik.ali 87
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
88
		model.addAttribute("schemes", schemes);
89
		return "schemes-paginated";
90
	}
91
 
92
	@RequestMapping(value = "/getSchemeById", method = RequestMethod.GET)
22927 ashik.ali 93
	public String getSchemeById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, Model model)  throws ProfitMandiBusinessException{
22860 ashik.ali 94
		Scheme scheme = schemeService.getSchemeById(schemeId);
95
		model.addAttribute("scheme", scheme);
96
		return "scheme-details";
97
	}
98
 
99
	@RequestMapping(value = "/activeSchemeById", method = RequestMethod.PUT)
22927 ashik.ali 100
	public String activeSchemeById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)  throws ProfitMandiBusinessException{
22860 ashik.ali 101
		schemeService.activeSchemeById(schemeId);
102
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
103
		model.addAttribute("schemes", schemes);
104
		return "schemes-paginated";
105
	}
106
 
107
 
108
	@RequestMapping(value = "/expireSchemeById", method = RequestMethod.PUT)
22927 ashik.ali 109
	public String expireSchemeById(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.SCHEME_ID) int schemeId, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model)  throws ProfitMandiBusinessException{
22860 ashik.ali 110
		schemeService.expireSchemeById(schemeId);
111
		List<Scheme> schemes = schemeRepository.selectAll(offset, limit);
112
		model.addAttribute("schemes", schemes);
113
		return "schemes-paginated";
114
	}
115
}