diff --git a/build.gradle.kts b/build.gradle.kts index e31ffa8..9b3ab58 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -49,6 +49,7 @@ dependencies { annotationProcessor("org.projectlombok:lombok") testImplementation("org.springframework.boot:spring-boot-starter-test") testRuntimeOnly("org.junit.platform:junit-platform-launcher") + implementation ("org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.2") } diff --git a/src/main/java/com/m/linshor/config/SwaggerConfig.java b/src/main/java/com/m/linshor/config/SwaggerConfig.java new file mode 100644 index 0000000..80d7279 --- /dev/null +++ b/src/main/java/com/m/linshor/config/SwaggerConfig.java @@ -0,0 +1,17 @@ +package com.m.linshor.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.info.Info; + +@Configuration +public class SwaggerConfig { + @Bean + public OpenAPI customOpenAPI() { + return new OpenAPI() + .info(new Info().title("LinShor URL Shortener API") + .version("1.0") + .description("API documentation for the URL shortener service")); + } +} diff --git a/src/main/java/com/m/linshor/controllers/LinShorController.java b/src/main/java/com/m/linshor/controllers/LinShorController.java index df51612..365131f 100644 --- a/src/main/java/com/m/linshor/controllers/LinShorController.java +++ b/src/main/java/com/m/linshor/controllers/LinShorController.java @@ -3,6 +3,12 @@ import com.m.linshor.entities.Mapping; import com.m.linshor.services.LinShorService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.tags.Tag; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; + import java.net.URI; import java.net.URL; import java.util.Optional; @@ -15,27 +21,39 @@ @RestController @RequestMapping("/linshor/v1") @AllArgsConstructor +@Tag(name = "URL Shortener API", description = "Operations for URL shortening and redirection") public class LinShorController { private final LinShorService linShorService; - @GetMapping("/find/{shortUrl}") - public Optional findByShortUrl(@PathVariable String shortUrl) { - return linShorService.findByShortUrl(shortUrl); - } - - @PutMapping("update") - public Mapping updateLink(String longUrl) { + @PutMapping("/update") + @Operation(summary = "Update a long URL", + description = "Updates an existing shortened URL with a new long URL.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "Successfully updated"), + @ApiResponse(responseCode = "400", description = "Invalid URL format") + }) + public Mapping updateLink(@RequestParam @Parameter(description = "New long URL") String longUrl) { return linShorService.updateLink(longUrl); } @DeleteMapping("delete/{shortUrl}") - public int deleteByShortUrl(@PathVariable String shortUrl) { + @Operation(summary = "Delete a shortened URL", description = "Removes a shortened URL from the database.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "Successfully deleted"), + @ApiResponse(responseCode = "404", description = "Short URL not found") + }) + public int deleteByShortUrl(@PathVariable @Parameter(description = "Short URL to delete") String shortUrl) { linShorService.deleteByShortUrl(shortUrl); return 200; } @PostMapping("/post") - public ResponseEntity linshorUrl(@RequestBody String longUrl, HttpServletRequest request) { + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "Short URL created"), + @ApiResponse(responseCode = "400", description = "Invalid input") + }) + public ResponseEntity linshorUrl(@RequestBody @Parameter( + description = "The long URL to shorten", required = true) String longUrl, HttpServletRequest request) { Mapping mapping = linShorService.saveLink(longUrl); String serverAddress = request.getRequestURL().toString().replace(request.getRequestURI(), ""); @@ -45,11 +63,19 @@ public ResponseEntity linshorUrl(@RequestBody String longUrl, HttpServle } @GetMapping("/{shortUrl}") - public ResponseEntity redirectToLongUrl(@PathVariable String shortUrl) { + @Operation(summary = "Redirect to long URL", + description = "Finds the original URL for a given short URL and redirects.") + @ApiResponses({ + @ApiResponse(responseCode = "302", description = "Redirected successfully"), + @ApiResponse(responseCode = "404", description = "Short URL not found"), + @ApiResponse(responseCode = "400", description = "Invalid URL stored in database") + }) + public ResponseEntity redirectToLongUrl(@PathVariable @Parameter( + description = "Short URL to resolve") String shortUrl) { Optional mapping = linShorService.findByShortUrl(shortUrl); if (mapping.isPresent()) { - String longUrl = mapping.get().getLongUrl().trim().replaceAll("^\"|\"$", ""); // Remove surrounding quotes + String longUrl = mapping.get().getLongUrl().trim().replaceAll("^\"|\"$", ""); try { URI uri = new URL(longUrl).toURI(); // Convert URL to URI safely