Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
21292 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
21448 ashik.ali 3
import java.util.Set;
4
 
21292 ashik.ali 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;
22355 ashik.ali 10
import org.springframework.beans.factory.annotation.Qualifier;
21292 ashik.ali 11
import org.springframework.http.ResponseEntity;
12
import org.springframework.stereotype.Controller;
21692 amit.gupta 13
import org.springframework.transaction.annotation.Transactional;
21309 ashik.ali 14
import org.springframework.web.bind.annotation.RequestBody;
21292 ashik.ali 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.ResponseCodeHolder;
20
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
21
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21463 ashik.ali 22
import com.spice.profitmandi.common.model.UserInfo;
21740 ashik.ali 23
import com.spice.profitmandi.common.web.util.ResponseSender;
21735 ashik.ali 24
import com.spice.profitmandi.dao.entity.dtr.Brand;
25
import com.spice.profitmandi.dao.entity.dtr.Document;
26
import com.spice.profitmandi.dao.entity.dtr.Retailer;
27
import com.spice.profitmandi.dao.entity.dtr.RetailerBrand;
28
import com.spice.profitmandi.dao.entity.dtr.RetailerRegisteredAddress;
29
import com.spice.profitmandi.dao.entity.dtr.Shop;
30
import com.spice.profitmandi.dao.entity.dtr.ShopAddress;
31
import com.spice.profitmandi.dao.entity.dtr.User;
21768 amit.gupta 32
import com.spice.profitmandi.dao.entity.dtr.UserAccounts;
33
import com.spice.profitmandi.dao.entity.dtr.UserRole;
21735 ashik.ali 34
import com.spice.profitmandi.dao.entity.user.Address;
22355 ashik.ali 35
import com.spice.profitmandi.dao.entity.user.Cart;
36
import com.spice.profitmandi.dao.entity.user.Counter;
37
import com.spice.profitmandi.dao.entity.user.PrivateDealUser;
38
import com.spice.profitmandi.dao.entity.user.PrivateDealUserAddressId;
39
import com.spice.profitmandi.dao.entity.user.PrivateDealUserAddressMapping;
21768 amit.gupta 40
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
41
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
21735 ashik.ali 42
import com.spice.profitmandi.dao.repository.dtr.BrandRepository;
43
import com.spice.profitmandi.dao.repository.dtr.DocumentRepository;
44
import com.spice.profitmandi.dao.repository.dtr.RetailerAddressRepository;
45
import com.spice.profitmandi.dao.repository.dtr.RetailerBrandRepository;
46
import com.spice.profitmandi.dao.repository.dtr.RetailerRegisteredAddressRepository;
47
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
48
import com.spice.profitmandi.dao.repository.dtr.ShopAddressRepository;
49
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;
21768 amit.gupta 50
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
21735 ashik.ali 51
import com.spice.profitmandi.dao.repository.dtr.UserRepository;
21768 amit.gupta 52
import com.spice.profitmandi.dao.repository.dtr.UserRoleRepository;
21735 ashik.ali 53
import com.spice.profitmandi.dao.repository.user.AddressRepository;
22355 ashik.ali 54
import com.spice.profitmandi.dao.repository.user.CartRepository;
55
import com.spice.profitmandi.dao.repository.user.CounterRepository;
56
import com.spice.profitmandi.dao.repository.user.PrivateDealUserAddressMappingRepository;
57
import com.spice.profitmandi.dao.repository.user.PrivateDealUserRepository;
21431 ashik.ali 58
import com.spice.profitmandi.web.req.Category;
21426 ashik.ali 59
import com.spice.profitmandi.web.req.CreateRetailerAddressRequest;
60
import com.spice.profitmandi.web.req.CreateRetailerRequest;
61
import com.spice.profitmandi.web.req.RetailerAddBrandRequest;
21292 ashik.ali 62
 
22355 ashik.ali 63
import in.shop2020.model.v1.user.CartStatus;
21302 ashik.ali 64
import io.swagger.annotations.ApiImplicitParam;
65
import io.swagger.annotations.ApiImplicitParams;
66
import io.swagger.annotations.ApiOperation;
67
 
21292 ashik.ali 68
@Controller
22037 amit.gupta 69
@Transactional(rollbackFor=Throwable.class)
21292 ashik.ali 70
public class RetailerController {
71
 
72
	private static final Logger LOGGER=LoggerFactory.getLogger(RetailerController.class);
73
 
74
	@Autowired
22355 ashik.ali 75
	@Qualifier("userRepository")
21463 ashik.ali 76
	UserRepository userRepository;
77
 
78
	@Autowired
22355 ashik.ali 79
	@Qualifier("userUserRepository")
80
	com.spice.profitmandi.dao.repository.user.UserRepository userUserRepository;
81
 
82
	@Autowired
21768 amit.gupta 83
	UserRoleRepository userRoleRepository;
84
 
85
	@Autowired
21440 ashik.ali 86
	ResponseSender<?> responseSender;
87
 
88
	@Autowired
21292 ashik.ali 89
	RetailerRepository retailerRepository;
90
 
21390 ashik.ali 91
	@Autowired
92
	BrandRepository brandRepository;
93
 
94
	@Autowired
95
	AddressRepository addressRepository;
96
 
97
	@Autowired
98
	DocumentRepository documentRepository;
99
 
100
	@Autowired
101
	ShopRepository shopRepository;
102
 
103
	@Autowired
21768 amit.gupta 104
	UserAccountRepository userAccountRepository;
105
 
106
	@Autowired
21390 ashik.ali 107
	RetailerRegisteredAddressRepository retailerRegisteredAddressRepository;
108
 
109
	@Autowired
21426 ashik.ali 110
	RetailerAddressRepository retailerAddressRepository;
111
 
112
	@Autowired
21390 ashik.ali 113
	ShopAddressRepository shopAddressRepository;
114
 
115
	@Autowired
116
	RetailerBrandRepository retailerBrandRepository;
117
 
22355 ashik.ali 118
	@Autowired
119
	CounterRepository counterRepository;
120
 
121
	@Autowired
122
	PrivateDealUserRepository privateDealUserRepository;
123
 
124
	@Autowired
125
	PrivateDealUserAddressMappingRepository privateDealUserAddressMappingRepository;
126
 
127
	@Autowired
128
	CartRepository cartRepository;
129
 
21302 ashik.ali 130
	@ApiImplicitParams({
131
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
132
				required = true, dataType = "string", paramType = "header")
133
	})
21309 ashik.ali 134
 
21302 ashik.ali 135
	@ApiOperation(value = "Create Retailer")
136
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER, method=RequestMethod.POST)
22355 ashik.ali 137
	public ResponseEntity<?> createRetailer(HttpServletRequest request, @RequestBody CreateRetailerRequest createRetailerRequest) throws ProfitMandiBusinessException{
138
		LOGGER.info("request received with body [{}] at url {{}}: ", createRetailerRequest, request.getRequestURL().toString());
21440 ashik.ali 139
		try{
21691 amit.gupta 140
			UserInfo userInfo = (UserInfo)request.getAttribute("userInfo");
22355 ashik.ali 141
			LOGGER.info("requestAttribute [userInfo={}]", userInfo);
21463 ashik.ali 142
			User user = userRepository.selectById(userInfo.getUserId());
21768 amit.gupta 143
			this.createRetailer(user, createRetailerRequest);
21440 ashik.ali 144
			return responseSender.ok(ResponseCodeHolder.getMessage("RTLR_OK_1000"));
145
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
146
			LOGGER.error("ProfitMandi error: ", profitMandiBusinessException);
22355 ashik.ali 147
			throw profitMandiBusinessException;
148
			//return responseSender.badRequest(profitMandiBusinessException);
21440 ashik.ali 149
		}
150
	}
151
 
21768 amit.gupta 152
	private void createRetailer(User user, CreateRetailerRequest createRetailerRequest) 
21440 ashik.ali 153
			throws ProfitMandiBusinessException{
21390 ashik.ali 154
		Retailer retailer = new Retailer();
21426 ashik.ali 155
		retailer.setName(createRetailerRequest.getName());
156
		retailer.setNumber(createRetailerRequest.getNumber());
157
		retailer.setType(createRetailerRequest.getType());
158
		retailer.setMonthlySaleValue(createRetailerRequest.getMonthlySaleValue());
159
		retailer.setSmartphoneSaleValue(createRetailerRequest.getSmartphoneSaleValue());
160
		retailer.setRecharge(createRetailerRequest.getLineOfBusiness().isRecharge());
161
		retailer.setMobile(createRetailerRequest.getLineOfBusiness().isMobile());
162
		retailer.setAccessories(createRetailerRequest.getLineOfBusiness().isAccessories());
21431 ashik.ali 163
		retailer.setOther1(createRetailerRequest.getLineOfBusiness().getOther1());
164
		retailer.setOther2(createRetailerRequest.getLineOfBusiness().getOther2());
21440 ashik.ali 165
		Document retailerDocument = documentRepository.selectById(createRetailerRequest.getDocumentId());
166
		if(retailerRepository.isExistByDocumentId(retailerDocument.getId())){
167
			LOGGER.error("documet is already mapped with another retailer");
21448 ashik.ali 168
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, retailerDocument.getId(), "DCMNT_1000");
21440 ashik.ali 169
		}
170
		retailer.setDocumentId(retailerDocument.getId());
171
		final Document shopDocument = documentRepository.selectById(createRetailerRequest.getShop().getDocumentId());
172
		if(shopRepository.isExistByDocumentId(shopDocument.getId())){
21448 ashik.ali 173
			throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, shopDocument.getId(), "DCMNT_1000");
21440 ashik.ali 174
		}
21463 ashik.ali 175
		documentRepository.markDocumentAsPersisted(retailerDocument.getId());
21448 ashik.ali 176
		if(retailerRepository.isExistByNumberAndType(retailer.getNumber(), retailer.getType())){
177
			throw new ProfitMandiBusinessException(ProfitMandiConstants.NUMBER + ", " + ProfitMandiConstants.TYPE, retailer.getNumber() + ", " + retailer.getType(), "RTLR_1001");
178
		}
21463 ashik.ali 179
		documentRepository.markDocumentAsPersisted(shopDocument.getId());
22355 ashik.ali 180
 
181
		com.spice.profitmandi.dao.entity.user.User saholicUser = null;
182
		boolean foundRetailer = false;
183
		saholicUser = userUserRepository.selectByEmailId(user.getEmailId());
184
		if(saholicUser == null){
185
			Cart cart = new Cart();
186
			cart.setCartStatus(CartStatus.ACTIVE);
187
			cartRepository.persist(cart);
188
			saholicUser = new com.spice.profitmandi.dao.entity.user.User();
189
			saholicUser.setEmailId(user.getEmailId());
190
			saholicUser.setName(createRetailerRequest.getName());
191
			saholicUser.setActiveCartId(cart.getId());
192
			userUserRepository.persist(saholicUser);
193
		}else{
194
			foundRetailer = true;
195
		}
196
		//in.shop2020.model.v1.user.User saholicUser = Utils.createSaholicUser(user.getEmailId()); 
197
		retailer.setId(saholicUser.getId());
21440 ashik.ali 198
		retailerRepository.persist(retailer);
22355 ashik.ali 199
 
21440 ashik.ali 200
		Shop shop = new Shop();
201
		shop.setName(createRetailerRequest.getShop().getName());
202
		shop.setDocumentId(shopDocument.getId());
203
		shop.setRetailerId(retailer.getId());
204
		shopRepository.persist(shop);
21448 ashik.ali 205
		this.addBrandWithRetailer(retailer.getId(), createRetailerRequest.getCategories());
21440 ashik.ali 206
		final Address addressRetailer = this.createAddress(createRetailerRequest.getAddress());
21706 amit.gupta 207
		addressRetailer.setRetaierId(retailer.getId());
22041 amit.gupta 208
		addressRepository.persist(addressRetailer);
22355 ashik.ali 209
 
21440 ashik.ali 210
		final RetailerRegisteredAddress retailerRegisteredAddress = new RetailerRegisteredAddress();
211
		retailerRegisteredAddress.setRetailerId(retailer.getId());
212
		retailerRegisteredAddress.setAddressId(addressRetailer.getId());
213
		retailerRegisteredAddressRepository.persist(retailerRegisteredAddress);
21463 ashik.ali 214
		if(!createRetailerRequest.isShopAddressSameAsRetailerAddress() && createRetailerRequest.getAddress() == null){
215
			throw new ProfitMandiBusinessException(ProfitMandiConstants.ADDRESS, "", "");
21440 ashik.ali 216
		}
21463 ashik.ali 217
		if(createRetailerRequest.isShopAddressSameAsRetailerAddress()){
218
			ShopAddress shopAddress = new ShopAddress();
219
			shopAddress.setShopId(shop.getId());
220
			shopAddress.setAddressId(addressRetailer.getId());
221
			shopAddressRepository.persist(shopAddress);
222
		}else{
223
			final Address addressShop = this.createAddress(createRetailerRequest.getShop().getAddress());
22041 amit.gupta 224
			addressRepository.persist(addressShop);
21463 ashik.ali 225
			ShopAddress shopAddress = new ShopAddress();
226
			shopAddress.setShopId(shop.getId());
227
			shopAddress.setAddressId(addressShop.getId());
228
			shopAddressRepository.persist(shopAddress);
229
		}
21768 amit.gupta 230
		UserAccounts ua = new UserAccounts();
22355 ashik.ali 231
		ua.setAccount_key(String.valueOf(saholicUser.getId()));
21768 amit.gupta 232
		ua.setUser_id(user.getId());
21769 amit.gupta 233
		ua.setAccount_type(AccountType.saholic);
21768 amit.gupta 234
		userAccountRepository.persist(ua);
235
 
236
		UserAccounts ua2 = new UserAccounts();
237
		ua2.setAccount_key(String.valueOf(saholicUser.getActiveCartId()));
238
		ua2.setUser_id(user.getId());
239
		ua2.setAccount_type(AccountType.cartId);
240
		userAccountRepository.persist(ua2);
241
 
242
		UserRole ur = new UserRole();
22011 ashik.ali 243
		ur.setRoleType(RoleType.RETAILER);
21768 amit.gupta 244
		ur.setUserId(user.getId());
245
		userRoleRepository.persist(ur);
22355 ashik.ali 246
 
247
		if(foundRetailer){
248
			LOGGER.info("\n\n\n****retailer found\n\n\n");
249
			PrivateDealUser privateDealUser = privateDealUserRepository.selectById(saholicUser.getId());
250
			if(privateDealUser.getCounterId() == null){
251
				Integer counterId = this.createCounter(user.getEmailId(), createRetailerRequest.getGstNumber(), user.getMobileNumber(), retailer.getName(), addressRetailer.getId());
252
				privateDealUser.setCounterId(counterId);
253
				privateDealUserRepository.persist(privateDealUser);
254
			}
255
		}else{
256
			LOGGER.info("retailer not found");
257
			//gst number intergration with counter
258
			Integer counterId = this.createCounter(user.getEmailId(), createRetailerRequest.getGstNumber(), user.getMobileNumber(), retailer.getName(), addressRetailer.getId());
259
 
260
 
261
				PrivateDealUser privateDealUser = privateDealUserRepository.selectById(saholicUser.getId());
262
			if(privateDealUser != null){
263
				//LOGGER.info("PrivateDealUser found with id [{}]", saholicUser.getId());
264
				privateDealUser.setCounterId(counterId);
265
				privateDealUserRepository.update(privateDealUser);
266
			}else{	
267
				LOGGER.info("PrivateDealUser not found with id [{}]", saholicUser.getId());
268
				privateDealUser = new PrivateDealUser();
269
				privateDealUser.setActive(true);
270
				privateDealUser.setBulkShipmentAmountLimit(50000);
271
				privateDealUser.setId(saholicUser.getId());
272
				privateDealUser.setCounterId(counterId);
273
				privateDealUserRepository.persist(privateDealUser);
274
			}
275
			PrivateDealUserAddressMapping privateDealUserAddressMapping = new PrivateDealUserAddressMapping();
276
			PrivateDealUserAddressId privateDealUserAddressId = new PrivateDealUserAddressId();
277
			privateDealUserAddressId.setUserId(retailer.getId());
278
			privateDealUserAddressId.setAddressId(addressRetailer.getId());
279
			privateDealUserAddressMapping.setId(privateDealUserAddressId);
280
			privateDealUserAddressMappingRepository.persist(privateDealUserAddressMapping);
281
 
282
			saholicUser.setAddressId(addressRetailer.getId());
283
			userUserRepository.persist(saholicUser);
284
		}
21292 ashik.ali 285
	}
286
 
22355 ashik.ali 287
	private Integer createCounter(String emailId, String gstNumber, String mobileNumber, String name, int addressId){
288
		if(gstNumber != null && !gstNumber.isEmpty()){
289
			Counter counter = new Counter();
290
			counter.setEmailId(emailId);
291
			counter.setGstin(gstNumber);
292
			counter.setMobileNumber(mobileNumber);
293
			counter.setName(name);
294
			counter.setAddressId(addressId);
295
			counterRepository.persist(counter);
296
			return counter.getId();
297
		}else{
298
			return null;
299
		}
21463 ashik.ali 300
	}
22355 ashik.ali 301
 
302
 
21448 ashik.ali 303
	private void addBrandWithRetailer(int retailerId, Set<Category> categories)
304
		throws ProfitMandiBusinessException{
305
		for(Category category : categories){
306
			for(com.spice.profitmandi.web.req.Brand brandModel : category.getBrands()){
307
				Brand brand = brandRepository.selectByIdAndName(brandModel.getId(), brandModel.getName());
308
				final RetailerBrand retailerBrand = new RetailerBrand();
309
				retailerBrand.setRetailerId(retailerId);
310
				retailerBrand.setBrandId(brand.getId());
311
				retailerBrandRepository.persist(retailerBrand);
312
			}
313
		}
314
	}
21292 ashik.ali 315
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ALL,method=RequestMethod.GET)
21496 ashik.ali 316
	public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize){
21292 ashik.ali 317
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21496 ashik.ali 318
		return responseSender.ok(retailerRepository.selectAll(pageNumber, pageSize));
21292 ashik.ali 319
	}
320
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID, method=RequestMethod.GET)
21431 ashik.ali 321
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 322
		LOGGER.info("requested url : "+request.getRequestURL().toString());
323
		try {
21440 ashik.ali 324
			return responseSender.ok(retailerRepository.selectById(id));
325
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
326
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
327
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 328
		}
329
	}
330
 
331
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_NAME, method=RequestMethod.GET)
332
	public ResponseEntity<?> getByName(HttpServletRequest request, @RequestParam(name = "name") String name){
333
		LOGGER.info("requested url : "+request.getRequestURL().toString());
334
		try {
21440 ashik.ali 335
			return responseSender.ok(retailerRepository.selectByName(name));
336
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
337
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
338
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 339
		}
340
	}
341
 
342
 
343
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID,method=RequestMethod.DELETE)
21431 ashik.ali 344
	public ResponseEntity<?> removeById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 345
		LOGGER.info("requested url : "+request.getRequestURL().toString());
346
		try {
347
			retailerRepository.deleteById(id);
21440 ashik.ali 348
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
349
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
350
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
351
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 352
		}
353
	}
354
 
21426 ashik.ali 355
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ADD, method=RequestMethod.POST)
21431 ashik.ali 356
	public ResponseEntity<?> addShop(HttpServletRequest request, @RequestBody com.spice.profitmandi.web.req.Shop createShop, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 357
		LOGGER.info("requested url : "+request.getRequestURL().toString());
358
		try {
359
			Document document = documentRepository.selectById(createShop.getDocumentId());
360
			if(shopRepository.isExistByDocumentId(createShop.getDocumentId())){
361
				LOGGER.error("documet is already mapped with another shop");
362
				throw new ProfitMandiBusinessException(ProfitMandiConstants.DOCUMENT_ID, document.getId(), "");
363
			}
364
			retailerRepository.selectById(retailerId);
365
			Shop shop = new Shop();
366
			shop.setRetailerId(retailerId);
367
			Address address = this.createAddress(createShop.getAddress());
368
			addressRepository.persist(address);
369
			shop.setAddressId(address.getId());
370
			shop.setDocumentId(document.getId());
371
			shopRepository.persist(shop);
372
			ShopAddress shopAddress = new ShopAddress();
373
			shopAddress.setAddressId(address.getId());
374
			shopAddress.setShopId(shop.getId());
375
			shopAddressRepository.persist(shopAddress);
21440 ashik.ali 376
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
377
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
378
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
379
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 380
		}
381
	}
382
 
383
 
21302 ashik.ali 384
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 385
	public ResponseEntity<?> removeShop(HttpServletRequest request, @RequestParam(name = "shopId") int shopId, @RequestParam(name = "retailerId") int retailerId){
21292 ashik.ali 386
		LOGGER.info("requested url : "+request.getRequestURL().toString());
387
		try {
388
			retailerRepository.removeShop(shopId, retailerId);
21440 ashik.ali 389
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
390
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
391
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
392
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 393
		}
394
	}
395
 
21426 ashik.ali 396
	private Address createAddress(com.spice.profitmandi.web.req.Address createAddress){
397
		Address address = new Address();
398
		address.setName(createAddress.getName());
399
		address.setLine1(createAddress.getLine1());
400
		address.setLine2(createAddress.getLine2());
401
		address.setLandmark(createAddress.getLandmark());
402
		address.setCity(createAddress.getCity());
403
		address.setState(createAddress.getState());
404
		address.setPinCode(createAddress.getPinCode());
405
		address.setCountry(createAddress.getCountry());
406
		address.setPhoneNumber(createAddress.getPhoneNumber());
21706 amit.gupta 407
 
21426 ashik.ali 408
		return address;
409
	}
21440 ashik.ali 410
 
21426 ashik.ali 411
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_ADD, method=RequestMethod.POST)
412
	public ResponseEntity<?> addAddress(HttpServletRequest request, @RequestBody CreateRetailerAddressRequest createRetailerAddress){
413
		LOGGER.info("requested url : "+request.getRequestURL().toString());
414
		try {
415
			retailerRepository.addAddress(this.createAddress(createRetailerAddress.getAddress()), createRetailerAddress.getRetailerId());
21440 ashik.ali 416
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
417
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
418
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
419
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 420
		}
421
	}
21292 ashik.ali 422
 
21426 ashik.ali 423
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 424
	public ResponseEntity<?> removeAddress(HttpServletRequest request, @RequestParam(name = "addressId") int addressId, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 425
		LOGGER.info("requested url : "+request.getRequestURL().toString());
426
		try {
427
			retailerRepository.removeAddress(addressId, retailerId);
21440 ashik.ali 428
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
429
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
430
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
431
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 432
		}
433
	}
434
 
435
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_ADD, method=RequestMethod.POST)
436
	public ResponseEntity<?> addBrand(HttpServletRequest request, @RequestBody RetailerAddBrandRequest retailerAddBrandRequest){
437
		LOGGER.info("requested url : "+request.getRequestURL().toString());
438
		try {
21448 ashik.ali 439
			retailerRepository.selectById(retailerAddBrandRequest.getRetailerId());
440
			this.addBrandWithRetailer(retailerAddBrandRequest.getRetailerId(), retailerAddBrandRequest.getCategories());
21440 ashik.ali 441
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
442
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
443
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
444
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 445
		}
446
	}
447
 
448
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_REMOVE, method=RequestMethod.DELETE)
21431 ashik.ali 449
	public ResponseEntity<?> removeBrand(HttpServletRequest request, @RequestParam(name = "brandId") int brandId, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 450
		LOGGER.info("requested url : "+request.getRequestURL().toString());
451
		try {
452
			brandRepository.selectById(brandId);
453
			retailerRepository.selectById(retailerId);
21448 ashik.ali 454
			retailerBrandRepository.deleteByRetailerAndBrandId(retailerId, brandId);
21440 ashik.ali 455
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
456
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
457
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
458
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 459
		}
460
	}
461
 
462
 
21448 ashik.ali 463
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_DOCUMENT, method=RequestMethod.GET)
464
	public ResponseEntity<?> getDocumentById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){
21426 ashik.ali 465
		LOGGER.info("requested url : "+request.getRequestURL().toString());
466
		try {
21448 ashik.ali 467
			return responseSender.ok(retailerRepository.selectDocumentById(retailerId));
21440 ashik.ali 468
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
469
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
470
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 471
		}
472
	}
473
 
21448 ashik.ali 474
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_REGISTERED_ADDRESS, method=RequestMethod.GET)
475
	public ResponseEntity<?> getRegisteredAddressById(HttpServletRequest request, @RequestParam(name = "retailerId") int retailerId){
476
		LOGGER.info("requested url : "+request.getRequestURL().toString());
477
		try {
21698 amit.gupta 478
			return responseSender.ok(retailerRegisteredAddressRepository.selectAddressIdByRetailerId(retailerId));
21448 ashik.ali 479
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
480
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
481
			return responseSender.badRequest(profitMandiBusinessException);
482
		}
483
	}
484
 
21426 ashik.ali 485
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_ALL, method=RequestMethod.GET)
21431 ashik.ali 486
	public ResponseEntity<?> getAllShops(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 487
		LOGGER.info("requested url : "+request.getRequestURL().toString());
488
		try {
21440 ashik.ali 489
			return responseSender.ok(shopRepository.selectByRetailerId(id));
490
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
491
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
492
			return responseSender.badRequest(profitMandiBusinessException);
21426 ashik.ali 493
		}
494
	}
495
 
496
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ADDRESS_ALL, method=RequestMethod.GET)
21431 ashik.ali 497
	public ResponseEntity<?> getAllAddresses(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 498
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21440 ashik.ali 499
		return responseSender.ok(retailerAddressRepository.selectAddressesByRetailerId(id));
21426 ashik.ali 500
	}
501
 
502
 
503
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_BRAND_ALL, method=RequestMethod.GET)
21431 ashik.ali 504
	public ResponseEntity<?> getAllBrads(HttpServletRequest request, @RequestParam(name = "id") int id){
21426 ashik.ali 505
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21440 ashik.ali 506
		return responseSender.ok(retailerBrandRepository.selectBrandNamesByRetailerId(id));
21426 ashik.ali 507
	}
508
 
21292 ashik.ali 509
}