Skip to content

Commit

Permalink
Merge pull request #35 from nastiausenko/link-controller
Browse files Browse the repository at this point in the history
added link controller and create link method
  • Loading branch information
IvanShalaev1990 authored Apr 17, 2024
2 parents 8938a98 + 0cd9152 commit 8cb2688
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 2 deletions.
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;
}
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;
}
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);
}
}
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));
}
}
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;
}

0 comments on commit 8cb2688

Please sign in to comment.