-
Notifications
You must be signed in to change notification settings - Fork 1
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
querwurzel
committed
Nov 8, 2023
1 parent
b3849d8
commit 54fe6c4
Showing
9 changed files
with
103 additions
and
18 deletions.
There are no files selected for viewing
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
51 changes: 51 additions & 0 deletions
51
backend/src/main/java/com/github/binpastes/paste/api/model/SearchView.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,51 @@ | ||
package com.github.binpastes.paste.api.model; | ||
|
||
import com.github.binpastes.paste.domain.Paste; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
public record SearchView( | ||
List<SearchItemView> pastes | ||
) { | ||
|
||
public static SearchView of(List<SearchItemView> pastes) { | ||
return new SearchView(pastes); | ||
} | ||
|
||
public record SearchItemView( | ||
String id, | ||
String title, | ||
String highlight, | ||
int sizeInBytes, | ||
LocalDateTime dateCreated, | ||
LocalDateTime dateOfExpiry | ||
) { | ||
|
||
private static final short HIGHLIGHT_RANGE = 25; | ||
|
||
public static SearchItemView of(final Paste reference, final String term) { | ||
return new SearchItemView( | ||
reference.getId(), | ||
reference.getTitle(), | ||
highlight(reference.getContent(), term), | ||
reference.getContent().getBytes().length, | ||
reference.getDateCreated(), | ||
reference.getDateOfExpiry() | ||
); | ||
} | ||
|
||
public static String highlight(final String content, final String term) { | ||
final int idx = content.indexOf(term); | ||
|
||
if (idx == -1) { | ||
return content; | ||
} | ||
|
||
return content.substring( | ||
Math.max(0, idx - HIGHLIGHT_RANGE), | ||
Math.min(content.length(), idx + term.length() + HIGHLIGHT_RANGE) | ||
); | ||
} | ||
} | ||
} |
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
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
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,9 @@ | ||
|
||
export type PasteSearchView = { | ||
id: string | ||
title?: string | ||
highlight: string | ||
sizeInBytes: number | ||
dateCreated: string | ||
dateOfExpiry?: string | ||
} |
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