Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}


Expand Down
17 changes: 17 additions & 0 deletions src/main/java/com/m/linshor/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}
48 changes: 37 additions & 11 deletions src/main/java/com/m/linshor/controllers/LinShorController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Mapping> 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<String> linshorUrl(@RequestBody String longUrl, HttpServletRequest request) {
@ApiResponses({
@ApiResponse(responseCode = "200", description = "Short URL created"),
@ApiResponse(responseCode = "400", description = "Invalid input")
})
public ResponseEntity<String> 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(), "");
Expand All @@ -45,11 +63,19 @@ public ResponseEntity<String> linshorUrl(@RequestBody String longUrl, HttpServle
}

@GetMapping("/{shortUrl}")
public ResponseEntity<Object> 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<Object> redirectToLongUrl(@PathVariable @Parameter(
description = "Short URL to resolve") String shortUrl) {
Optional<Mapping> 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
Expand Down