package org.itevents.controller;

import org.itevents.model.Event;
import org.itevents.service.EventService;
import org.itevents.service.EventServiceImpl;
import org.itevents.service.RegLinkClickService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class RegLinkClickRestController {

    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    private EventService eventService = context.getBean("eventService", EventServiceImpl.class);
    private RegLinkClickService regLinkClickService = context.getBean("regLinkClickService", RegLinkClickService.class);

    @RequestMapping(value = "/reglinkclicks/{user_id}/{event_id}")
    public ResponseEntity<String> getEvent(@PathVariable("event_id") int eventId, @PathVariable("user_id") int userId) {
        Event event = eventService.getEvent(eventId);
        if (event == null) {
            return new ResponseEntity(HttpStatus.NOT_FOUND);
        }
        regLinkClickService.addClick(eventId, userId);
        return new ResponseEntity<String>(event.getRegLink(), HttpStatus.OK);
    }

}

@@ -319,32 +319,77 @@
 icationContext;%0A
+import org.springframework.http.HttpHeaders;%0A
 import org.sprin
@@ -652,16 +652,113 @@
 oller;%0A%0A
+import java.net.MalformedURLException;%0Aimport java.net.URISyntaxException;%0Aimport java.net.URL;%0A%0A
 %0A@RestCo
@@ -1123,16 +1123,17 @@
 lass);%0A%0A
+%0A
     @Req
@@ -1161,35 +1161,30 @@
 /reg
-linkclicks/%7Buser
+Links/%7Bevent
 _id%7D/%7B
-event
+user
 _id%7D
@@ -1473,131 +1473,678 @@
     
-regLinkClickService.addClick(eventId, userId);%0A        return new ResponseEntity%3CString%3E(event.getRegLink(), HttpStatus.OK)
+HttpHeaders headers = new HttpHeaders();%0A        if (!setLocation(event, headers)) %7B%0A            return new ResponseEntity(HttpStatus.NOT_FOUND);%0A        %7D%0A        regLinkClickService.addClick(eventId, userId);%0A        return new ResponseEntity(null, headers, HttpStatus.FOUND);%0A    %7D%0A%0A    private boolean setLocation(Event event, HttpHeaders headers) %7B%0A        try %7B%0A            headers.setLocation(new URL(event.getRegLink()).toURI());%0A        %7D catch (URISyntaxException e) %7B%0A            e.printStackTrace();%0A            return false;%0A        %7D catch (MalformedURLException e) %7B%0A            e.printStackTrace();%0A            return false;%0A        %7D%0A        return true
 ;%0A  

add redirect