-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
845 additions
and
1,005 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
64 changes: 64 additions & 0 deletions
64
apps/api/src/main/java/com/mally/api/pastebin/PastebinController.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,64 @@ | ||
package com.mally.api.pastebin; | ||
|
||
import com.mally.api.pastebin.dtos.CreatePasteDTO; | ||
import com.mally.api.pastebin.entities.Paste; | ||
import com.mally.api.pastebin.services.PastebinService; | ||
import com.mally.api.shared.rest.dtos.ApiResponseDTO; | ||
import jakarta.validation.Valid; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@AllArgsConstructor | ||
@RestController | ||
@RequestMapping("/pastebin") | ||
public class PastebinController { | ||
|
||
private final PastebinService pastebinService; | ||
|
||
@GetMapping("/paste/{slug}") | ||
public ResponseEntity<ApiResponseDTO> findPaste(@PathVariable String slug) { | ||
try { | ||
Optional<Paste> paste = pastebinService.findBySlug(slug); | ||
return paste | ||
.map(p -> ResponseEntity | ||
.ok() | ||
.body(ApiResponseDTO.success("Paste found successfully.", p))) | ||
.orElseGet(() -> ResponseEntity | ||
.badRequest() | ||
.body(ApiResponseDTO.error("Paste not found.", List.of()))); | ||
} catch (Exception e) { | ||
return ResponseEntity | ||
.internalServerError() | ||
.body( | ||
ApiResponseDTO.error( | ||
"An unexpected error occurred while finding the paste.", | ||
List.of() | ||
) | ||
); | ||
} | ||
} | ||
|
||
@PostMapping("/paste") | ||
public ResponseEntity<ApiResponseDTO> paste(@Valid @RequestBody CreatePasteDTO dto) { | ||
try { | ||
Paste paste = pastebinService.create(dto); | ||
|
||
return ResponseEntity | ||
.ok() | ||
.body(ApiResponseDTO.success("Paste created successfully.", paste)); | ||
} catch (Exception e) { | ||
return ResponseEntity | ||
.internalServerError() | ||
.body( | ||
ApiResponseDTO.error( | ||
"An unexpected error occurred while creating the paste.", | ||
List.of() | ||
) | ||
); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
apps/api/src/main/java/com/mally/api/pastebin/dtos/CreatePasteDTO.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,19 @@ | ||
package com.mally.api.pastebin.dtos; | ||
|
||
import com.mally.api.pastebin.entities.PasteSyntax; | ||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class CreatePasteDTO { | ||
@NotBlank | ||
private String text; | ||
|
||
@NotNull | ||
private PasteSyntax syntax; | ||
} |
46 changes: 46 additions & 0 deletions
46
apps/api/src/main/java/com/mally/api/pastebin/entities/Paste.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,46 @@ | ||
package com.mally.api.pastebin.entities; | ||
|
||
import jakarta.persistence.*; | ||
import jakarta.validation.constraints.NotNull; | ||
import lombok.*; | ||
import org.hibernate.annotations.ColumnDefault; | ||
import org.springframework.data.annotation.CreatedDate; | ||
|
||
import java.time.ZonedDateTime; | ||
|
||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Getter | ||
@Setter | ||
@Entity | ||
public class Paste { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "paste_id_seq") | ||
@SequenceGenerator(name = "paste_id_seq", sequenceName = "paste_id_seq", allocationSize = 1) | ||
private Long id; | ||
|
||
@Column(unique = true) | ||
@NotNull | ||
private String slug; | ||
|
||
@Column | ||
@NotNull | ||
private String text; | ||
|
||
@Column | ||
@Enumerated(EnumType.STRING) | ||
@ColumnDefault("PLAIN_TEXT") | ||
@NotNull | ||
private PasteSyntax syntax; | ||
|
||
@Column(name = "created_at") | ||
@CreatedDate | ||
@NotNull | ||
private ZonedDateTime createdAt; | ||
|
||
@Column(name = "expires_at") | ||
@NotNull | ||
private ZonedDateTime expiresAt; | ||
} | ||
|
17 changes: 17 additions & 0 deletions
17
apps/api/src/main/java/com/mally/api/pastebin/entities/PasteSyntax.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.mally.api.pastebin.entities; | ||
|
||
public enum PasteSyntax { | ||
PLAIN_TEXT, | ||
TYPESCRIPT, | ||
CSS, | ||
C, | ||
CPP, | ||
CSHARP, | ||
JAVA, | ||
GO, | ||
RUST, | ||
PYTHON, | ||
PHP, | ||
SHELL, | ||
RUBY | ||
} |
12 changes: 12 additions & 0 deletions
12
apps/api/src/main/java/com/mally/api/pastebin/repositories/PastebinRepository.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,12 @@ | ||
package com.mally.api.pastebin.repositories; | ||
|
||
import com.mally.api.pastebin.entities.Paste; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface PastebinRepository extends CrudRepository<Paste, Long> { | ||
Optional<Paste> findBySlug(String slug); | ||
} |
38 changes: 38 additions & 0 deletions
38
apps/api/src/main/java/com/mally/api/pastebin/services/PastebinService.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,38 @@ | ||
package com.mally.api.pastebin.services; | ||
|
||
import com.mally.api.pastebin.dtos.CreatePasteDTO; | ||
import com.mally.api.pastebin.entities.Paste; | ||
import com.mally.api.pastebin.repositories.PastebinRepository; | ||
import io.github.thibaultmeyer.cuid.CUID; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.ZonedDateTime; | ||
import java.util.Optional; | ||
|
||
@AllArgsConstructor | ||
@Service | ||
public class PastebinService { | ||
private static final Integer EXPIRES_IN_DAYS = 7; | ||
|
||
private final PastebinRepository pastebinRepository; | ||
|
||
public Optional<Paste> findBySlug(String slug) { | ||
return pastebinRepository.findBySlug(slug); | ||
} | ||
|
||
public Paste create(CreatePasteDTO dto) { | ||
final ZonedDateTime now = ZonedDateTime.now(); | ||
final CUID slug = CUID.randomCUID2(22); | ||
|
||
Paste paste = Paste.builder() | ||
.text(dto.getText()) | ||
.syntax(dto.getSyntax()) | ||
.slug(slug.toString()) | ||
.createdAt(now) | ||
.expiresAt(now.plusDays(EXPIRES_IN_DAYS)) | ||
.build(); | ||
|
||
return pastebinRepository.save(paste); | ||
} | ||
} |
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
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
2 changes: 2 additions & 0 deletions
2
apps/api/src/main/resources/db/changelog/db.changelog-master.yaml
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,3 +1,5 @@ | ||
databaseChangeLog: | ||
- include: | ||
file: "db/changelog/scripts/v1-add-url-table.yaml" | ||
- include: | ||
file: "db/changelog/scripts/v2-add-paste-table.yaml" |
41 changes: 41 additions & 0 deletions
41
apps/api/src/main/resources/db/changelog/scripts/v2-add-paste-table.yaml
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,41 @@ | ||
databaseChangeLog: | ||
- changeSet: | ||
id: 2-1 | ||
author: Neuman F. | ||
changes: | ||
- createTable: | ||
columns: | ||
- column: | ||
constraints: | ||
primaryKey: true | ||
primaryKeyName: paste_id_pkey | ||
name: id | ||
type: SERIAL | ||
- column: | ||
name: text | ||
type: TEXT | ||
constraints: | ||
nullable: false | ||
- column: | ||
name: slug | ||
type: VARCHAR(255) | ||
constraints: | ||
unique: true | ||
nullable: false | ||
- column: | ||
name: syntax | ||
type: VARCHAR(50) | ||
constraints: | ||
nullable: false | ||
- column: | ||
name: created_at | ||
type: DATETIME | ||
constraints: | ||
nullable: false | ||
defaultValueComputed: "CURRENT_TIMESTAMP" | ||
- column: | ||
name: expires_at | ||
type: DATETIME | ||
constraints: | ||
nullable: false | ||
tableName: paste |
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
Oops, something went wrong.