Skip to content

Commit

Permalink
исправление поиска по дате
Browse files Browse the repository at this point in the history
  • Loading branch information
ForthA committed Dec 15, 2024
1 parent fa5ba26 commit 8aebb5f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public ResponseEntity<Form> createForm(@RequestBody FormRequest formRequest){
form.setOwnerEmail(formRequest.getOwnerMail());

form.setName(formRequest.getName());
form.setDate(Date.from(Instant.now()).toString());
form.setDate(Instant.now());
form.setRedactors(new ArrayList<>());

return ResponseEntity.ok(formRepository.save(form));
Expand Down Expand Up @@ -99,12 +99,13 @@ public ResponseEntity<byte[]> getTable(
@GetMapping("/table")
public ResponseEntity<List<Form>> searchForm(
@RequestParam(value = "table_name", required = false, defaultValue = "") String tableName,
@RequestParam(value = "creation_date", required = false, defaultValue = "") String date,
@RequestParam(value = "from_date", required = false, defaultValue = "") String fromDate,
@RequestParam(value = "to_date", required = false, defaultValue = "") String toDate,
@RequestParam(value = "owner_mail", required = false, defaultValue = "") String owner,
@RequestParam(value = "redactor", required = false, defaultValue = "") String redactor
){
return ResponseEntity.ok(formService.getFormsSearch(
tableName, date, owner, redactor
tableName, fromDate, toDate, owner, redactor
));
}

Expand Down
3 changes: 2 additions & 1 deletion backend/src/main/java/ru/yandex/forms/model/Form.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.time.Instant;
import java.util.Date;
import java.util.List;

Expand All @@ -26,7 +27,7 @@ public class Form {

private String path;

private String date;
private Instant date;

private String tableName;

Expand Down
32 changes: 23 additions & 9 deletions backend/src/main/java/ru/yandex/forms/services/FormService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -128,16 +131,18 @@ public ResponseEntity<HttpStatus> patchRedactors(String formId, List<String> red
}


public List<Form> getFormsSearch(String tableName, String date, String owner, String redactor){

public List<Form> getFormsSearch(String tableName, String fromDate, String toDate, String owner, String redactor){
if (toDate.isBlank()){
toDate = "3000-12-20 14:02:55";
}
if (Objects.equals(redactor, "")){
return formRepository.findByNameLikeIgnoreCaseAndOwnerEmailLikeIgnoreCaseAndDateLikeIgnoreCase(
tableName, owner, date
return formRepository.findByNameLikeIgnoreCaseAndOwnerEmailLikeIgnoreCaseAndDateBetween(
tableName, owner, convertDate(fromDate), convertDate(toDate)
);
}
else {
return formRepository.findByNameLikeIgnoreCaseAndOwnerEmailLikeIgnoreCaseAndRedactorsContainsIgnoreCaseAndDateLikeIgnoreCase(
tableName, owner, redactor, date
return formRepository.findByNameLikeIgnoreCaseAndOwnerEmailLikeIgnoreCaseAndRedactorsContainsIgnoreCaseAndDateBetweenIgnoreCase(
tableName, owner, redactor, convertDate(fromDate), convertDate(toDate)
);
}

Expand Down Expand Up @@ -189,4 +194,13 @@ private MediaType getMediaType(String filePath) {
return MediaType.APPLICATION_OCTET_STREAM;
}

private Instant convertDate(String date){
String pattern = "yyyy-MM-dd HH:mm:ss";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern, Locale.UK);
LocalDateTime localDateTime = LocalDateTime.parse(date, dateTimeFormatter);
ZoneId zoneId = ZoneId.of("UTC");
ZonedDateTime zonedDateTime = localDateTime.atZone(zoneId);
return zonedDateTime.toInstant();
}

}
12 changes: 7 additions & 5 deletions backend/src/main/java/ru/yandex/forms/utils/DataLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import ru.yandex.forms.repositories.FormRepository;
import ru.yandex.forms.repositories.UserRepository;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Component
Expand Down Expand Up @@ -88,28 +90,28 @@ private void formsCreation(){
form1.setOwnerEmail(SASHA_MAIL);
form1.setRedactors(List.of(SENYA_MAIL));
form1.setPath("./backend/uploads/tables/" + form1.getTableName() + ".xlsx");
form1.setDate("07.12.2024");
form1.setDate(Instant.parse("2024-12-12T15:36:50.357Z"));
formRepository.save(form1);

}
if (formRepository.findByOwnerEmailAndName(SASHA_MAIL, "Домашние животные/2").isEmpty()){
Form form2 = new Form();
form2.setName("Домашние животные/2");
form2.setTableName("Домашние животные/2");
form2.setTableName("form2");
form2.setOwnerEmail(SASHA_MAIL);
form2.setRedactors(List.of(SENYA_MAIL, ALISA_MAIL, DENIS_MAIL));
form2.setPath("./backend/uploads/tables/" + form2.getTableName() + ".xlsx");
form2.setDate("06.12.2024");
form2.setDate(Instant.parse("2024-12-09T15:36:50.357Z"));
formRepository.save(form2);
}
if (formRepository.findByOwnerEmailAndName(SASHA_MAIL, "Домашние животные/3").isEmpty()){
Form form3 = new Form();
form3.setName("Домашние животные/3");
form3.setTableName("Домашние животные/3");
form3.setTableName("form3");
form3.setOwnerEmail(SASHA_MAIL);
form3.setRedactors(List.of(SENYA_MAIL, VLAS_MAIL));
form3.setPath("./backend/uploads/tables/" + form3.getTableName() + ".xlsx");
form3.setDate("05.12.2024");
form3.setDate(Instant.parse("2024-12-01T15:36:50.357Z"));
formRepository.save(form3);
}
}
Expand Down

0 comments on commit 8aebb5f

Please sign in to comment.