-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from nastiausenko/link-controller
added link controller and create link method
- Loading branch information
Showing
5 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
src/main/java/com/linkurlshorter/urlshortener/link/CreateLinkRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.linkurlshorter.urlshortener.link; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
/** | ||
* Represents a request object for creating a link. | ||
* <p> | ||
* This class encapsulates the data required to create a link, including the long URL. | ||
* </p> | ||
* | ||
* <p> | ||
* An instance of this class is typically used as a parameter in methods that create links. | ||
* </p> | ||
* | ||
* <p> | ||
* The long URL provided in the request is validated using the {@link UrlLongFormatValidator} annotation. | ||
* </p> | ||
* | ||
* @author Artem Poliakov | ||
* @version 1.0 | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CreateLinkRequest { | ||
@UrlLongFormatValidator | ||
private String longLink; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/linkurlshorter/urlshortener/link/CreateLinkResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.linkurlshorter.urlshortener.link; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
/** | ||
* A response class representing the result of a link creation request. | ||
* It contains information about any error that occurred during the creation process | ||
* and the generated short link if the creation was successful. | ||
* | ||
* @author Artem Poliakov | ||
* @version 1.0 | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CreateLinkResponse { | ||
private String error; | ||
private String shortLink; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/linkurlshorter/urlshortener/link/InternalServerLinkException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.linkurlshorter.urlshortener.link; | ||
/** | ||
* Indicating unexpected exceptions which do not depend on user | ||
* @author Artem Poliakov | ||
* @version 1.0 | ||
* */ | ||
public class InternalServerLinkException extends RuntimeException{ | ||
private static final String DEFAULT_MSG = "Unexpected server error occurred"; | ||
|
||
public InternalServerLinkException() { | ||
super(DEFAULT_MSG); | ||
} | ||
|
||
public InternalServerLinkException(String msg) { | ||
super(msg); | ||
} | ||
} |
64 changes: 62 additions & 2 deletions
64
src/main/java/com/linkurlshorter/urlshortener/link/LinkController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,67 @@ | ||
package com.linkurlshorter.urlshortener.link; | ||
|
||
import org.springframework.stereotype.Controller; | ||
import com.linkurlshorter.urlshortener.user.User; | ||
import com.linkurlshorter.urlshortener.user.UserService; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.commons.lang3.RandomStringUtils; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@Controller | ||
import java.time.LocalDateTime; | ||
import java.util.UUID; | ||
/** | ||
* Controller for Link-related operations such as create, delete, update and get info + statistics | ||
* @version 1.0 | ||
* @author Artem Poliakov | ||
* */ | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/link") | ||
public class LinkController { | ||
private static final int SHORT_LINK_LIFETIME_IN_DAYS = 30; | ||
|
||
private final LinkService linkService; | ||
private final UserService userService; | ||
private final EntityManager entityManager; | ||
/** | ||
* Controller method for creating a new link. | ||
* <p> | ||
* This method handles POST requests to create a new link. It accepts a valid {@link CreateLinkRequest} | ||
* in the request body and generates a new short URL for the provided long URL. The short URL is then | ||
* associated with the authenticated user and stored in the database. Upon successful creation of the link, | ||
* a {@link CreateLinkResponse} containing the newly generated short URL is returned with a status of 200 (OK). | ||
* If any errors occur during the process, an {@link InternalServerLinkException} is thrown. | ||
* </p> | ||
* | ||
* @param createRequest the request object containing the long URL to be shortened | ||
* @return a ResponseEntity containing the response object indicating the success or failure of the link creation, | ||
* along with the generated short URL | ||
* @throws InternalServerLinkException if an error occurs during the link creation process | ||
* @see CreateLinkRequest | ||
* @see CreateLinkResponse | ||
* @see InternalServerLinkException | ||
*/ | ||
@PostMapping("/create") | ||
public ResponseEntity<CreateLinkResponse> createLink(@RequestBody @Valid CreateLinkRequest createRequest){ | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
UUID userId = userService.findByEmail(authentication.getName()).getId(); | ||
String newShortUrl = RandomStringUtils.randomAlphanumeric(8); | ||
try { | ||
linkService.save( | ||
Link.builder() | ||
.longLink(createRequest.getLongLink()) | ||
.shortLink(newShortUrl) | ||
.expirationTime(LocalDateTime.now().plusDays(SHORT_LINK_LIFETIME_IN_DAYS)) | ||
.user(entityManager.getReference(User.class, userId)) | ||
.build() //TODO: add validations (short link being unique etc) | ||
); | ||
} catch(Exception e){ | ||
throw new InternalServerLinkException(); | ||
} | ||
return ResponseEntity.ok(new CreateLinkResponse("ok", newShortUrl)); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/com/linkurlshorter/urlshortener/link/LinkModifyingResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.linkurlshorter.urlshortener.link; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
/** | ||
* Represents a response object for modifying a link. | ||
* <p> | ||
* This class encapsulates the response data after attempting to modify a link, | ||
* including any error messages that may occur during the process. | ||
* </p> | ||
* | ||
* <p> | ||
* An instance of this class is typically returned from methods that modify links, | ||
* providing information about the success or failure of the operation. | ||
* </p> | ||
* | ||
* <p> | ||
* The {@code error} field contains any error message that occurred during the modification process. | ||
* </p> | ||
* | ||
* @author [Author Name] | ||
* @version 1.0 | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class LinkModifyingResponse { | ||
private String error; | ||
} |