Skip to content

Commit

Permalink
Merge pull request #3 from APSN4/feature/date-format
Browse files Browse the repository at this point in the history
feature: new format date and time
  • Loading branch information
nuromirzak authored Jul 4, 2024
2 parents 6ecea4d + 53f9a23 commit 0aa0ca5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package codes.sharing.sharingcodes.controller;

import codes.sharing.sharingcodes.dto.DateDTO;
import codes.sharing.sharingcodes.model.Code;
import codes.sharing.sharingcodes.service.CodeService;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -8,6 +9,7 @@
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Optional;

@Controller
public class GetController {
Expand All @@ -21,7 +23,9 @@ public GetController(@Autowired CodeService codeService) {
public String getNthCode(@PathVariable String N, Model model) {
try {
Code currentCode = codeService.getById(N);
DateDTO dateDTO = codeService.formatDate(currentCode.getDate());
model.addAttribute("pieceOfCode", currentCode);
model.addAttribute("dateDTO", dateDTO);
return "getcode";
} catch (Exception e) {
model.addAttribute("error", e.getMessage());
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/codes/sharing/sharingcodes/dto/DateDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package codes.sharing.sharingcodes.dto;

public class DateDTO {

public DateDTO(String dateFormat, String timeFormat) {
this.dateFormat = dateFormat;
this.timeFormat = timeFormat;
}

private String dateFormat;
private String timeFormat;

public String getDateFormat() {
return dateFormat;
}

public void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
}

public String getTimeFormat() {
return timeFormat;
}

public void setTimeFormat(String timeFormat) {
this.timeFormat = timeFormat;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package codes.sharing.sharingcodes.service;

import codes.sharing.sharingcodes.dto.DateDTO;
import codes.sharing.sharingcodes.model.Code;

import java.util.List;
Expand All @@ -17,4 +18,6 @@ public interface CodeService {
public void deleteById(String id);

public List<Code> superGetAll();

public DateDTO formatDate(String date);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package codes.sharing.sharingcodes.service;

import codes.sharing.sharingcodes.dto.DateDTO;
import codes.sharing.sharingcodes.exceptions.NotFoundSnippet;
import codes.sharing.sharingcodes.model.Code;
import codes.sharing.sharingcodes.repository.CodeRepository;
Expand All @@ -10,6 +11,8 @@
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
public class CodeServiceImpl implements CodeService{
Expand Down Expand Up @@ -92,4 +95,24 @@ private void refresh(Code code) {

repo.save(code);
}

@Override
public DateDTO formatDate(String date) throws IllegalArgumentException {
String dateFormat;
String timeFormat;

Pattern patternDate = Pattern.compile("^.*?(?=T)");
Matcher matcherDate = patternDate.matcher(date);
Pattern patternTime = Pattern.compile("(?<=T)([^.]+)");
Matcher matcherTime = patternTime.matcher(date);

if (matcherDate.find() && matcherTime.find()) {
dateFormat = matcherDate.group();
timeFormat = matcherTime.group();
} else {
throw new IllegalArgumentException(String.format("Invalid string date: %s", date));
}

return new DateDTO(dateFormat, timeFormat);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/templates/getcode.ftlh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

<div class="container p-3">
<h1 class="display-6">${pieceOfCode.getId()}</h1>
<span id="load_date">${pieceOfCode.getDate()}</span>
<span id="load_date">Date: ${dateDTO.getDateFormat()}, time: ${dateDTO.getTimeFormat()}</span>
<#if pieceOfCode.isTimeLimit()>
<br>
<small>The code will be available for <u><span id="time_restriction">${pieceOfCode.getTime()}</span></u> seconds</small>
Expand Down

0 comments on commit 0aa0ca5

Please sign in to comment.