|
| 1 | +package com.jashgopani.urlshortner.slug.controller; |
| 2 | + |
| 3 | +import org.springframework.web.bind.annotation.RestController; |
| 4 | +import org.springframework.web.servlet.view.RedirectView; |
| 5 | + |
| 6 | +import com.jashgopani.urlshortner.slug.model.Slug; |
| 7 | +import com.jashgopani.urlshortner.slug.service.SlugService; |
| 8 | +import com.jashgopani.urlshortner.slug.utils.SlugConstants; |
| 9 | + |
| 10 | +import jakarta.servlet.http.HttpServletResponse; |
| 11 | + |
| 12 | +import java.nio.charset.MalformedInputException; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Objects; |
| 17 | + |
| 18 | +import org.springframework.beans.factory.annotation.Autowired; |
| 19 | +import org.springframework.http.HttpStatus; |
| 20 | +import org.springframework.http.HttpStatusCode; |
| 21 | +import org.springframework.http.ResponseEntity; |
| 22 | +import org.springframework.web.bind.annotation.GetMapping; |
| 23 | +import org.springframework.web.bind.annotation.PathVariable; |
| 24 | +import org.springframework.web.bind.annotation.PostMapping; |
| 25 | +import org.springframework.web.bind.annotation.RequestParam; |
| 26 | +import org.springframework.web.bind.annotation.RequestBody; |
| 27 | + |
| 28 | +@RestController |
| 29 | +public class SlugController { |
| 30 | + |
| 31 | + @Autowired |
| 32 | + private SlugService slugService; |
| 33 | + |
| 34 | + @GetMapping("/") |
| 35 | + public String index() { |
| 36 | + return "URL Shortner is running!"; |
| 37 | + } |
| 38 | + |
| 39 | + @PostMapping("/") |
| 40 | + public ResponseEntity<?> shortenURL(@RequestParam("url") String url) { |
| 41 | + try { |
| 42 | + Slug slug = slugService.generateSlug(url); |
| 43 | + return ResponseEntity.status(HttpStatus.CREATED).body(slug); |
| 44 | + } catch (Exception e) { |
| 45 | + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(getErrorMessage(SlugConstants.ERROR_INVALID_URL)); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + @GetMapping("/{slug}") |
| 50 | + public RedirectView redirectToURL(@PathVariable String slug) { |
| 51 | + try { |
| 52 | + Slug slugObj = slugService.getSlug(slug); |
| 53 | + if (Objects.isNull(slugObj)) { |
| 54 | + throw new NullPointerException(); |
| 55 | + } |
| 56 | + return new RedirectView(slugObj.getUrl()); |
| 57 | + } catch (Exception e) { |
| 58 | + |
| 59 | + RedirectView redirectView = new RedirectView("/error"); |
| 60 | + redirectView.setStatusCode(HttpStatus.NOT_FOUND); |
| 61 | + return redirectView; |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + @GetMapping("/slugs") |
| 67 | + public List<Slug> findAllSlugs() { |
| 68 | + return slugService.findAll(); |
| 69 | + } |
| 70 | + |
| 71 | + private Map<String, String> getErrorMessage(String message) { |
| 72 | + Map<String, String> error = new HashMap<>(); |
| 73 | + error.put("error", message); |
| 74 | + return error; |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments