Merge branch '82-paper-formatted-list' into 'dev'

Resolve "Метод в rest для список опубликованных статей"

Closes #82

See merge request romanov73/ng-tracker!45
This commit is contained in:
Anton Romanov 2019-04-05 09:42:30 +00:00
commit 0d7c594a94
5 changed files with 45 additions and 10 deletions

View File

@ -20,7 +20,6 @@ import ru.ulstu.user.error.UserNotActivatedException;
import ru.ulstu.user.error.UserNotFoundException; import ru.ulstu.user.error.UserNotFoundException;
import ru.ulstu.user.error.UserPasswordsNotValidOrNotMatchException; import ru.ulstu.user.error.UserPasswordsNotValidOrNotMatchException;
import ru.ulstu.user.error.UserResetKeyError; import ru.ulstu.user.error.UserResetKeyError;
import ru.ulstu.user.model.User;
import ru.ulstu.user.service.UserService; import ru.ulstu.user.service.UserService;
import java.util.Set; import java.util.Set;
@ -28,7 +27,6 @@ import java.util.stream.Collectors;
@ControllerAdvice @ControllerAdvice
public class AdviceController { public class AdviceController {
private final static String USER_NAME_TEMPLATE = "%s %s %s";
private final Logger log = LoggerFactory.getLogger(AdviceController.class); private final Logger log = LoggerFactory.getLogger(AdviceController.class);
private final UserService userService; private final UserService userService;
@ -38,11 +36,7 @@ public class AdviceController {
@ModelAttribute("currentUser") @ModelAttribute("currentUser")
public String getCurrentUser() { public String getCurrentUser() {
User user = userService.getCurrentUser(); return userService.getCurrentUser().getUserAbbreviate();
return String.format(USER_NAME_TEMPLATE,
user.getLastName(),
user.getFirstName().substring(0, 1),
user.getPatronymic().substring(0, 1));
} }
private Response<Void> handleException(ErrorConstants error) { private Response<Void> handleException(ErrorConstants error) {

View File

@ -51,7 +51,7 @@ public class PaperController {
@GetMapping("/dashboard") @GetMapping("/dashboard")
public void getDashboard(ModelMap modelMap) { public void getDashboard(ModelMap modelMap) {
modelMap.put("papers", paperService.findAllActive()); modelMap.put("papers", paperService.findAllActiveDto());
} }
@GetMapping("/paper") @GetMapping("/paper")

View File

@ -61,4 +61,9 @@ public class PaperRestController {
public Response<List<PaperDto>> filter(@RequestBody @Valid PaperFilterDto paperFilterDto) throws IOException { public Response<List<PaperDto>> filter(@RequestBody @Valid PaperFilterDto paperFilterDto) throws IOException {
return new Response<>(paperService.filter(paperFilterDto)); return new Response<>(paperService.filter(paperFilterDto));
} }
@GetMapping("formatted-list")
public Response<List<String>> getFormattedPaperList() {
return new Response<>(paperService.getFormattedPaperList());
}
} }

View File

@ -21,6 +21,7 @@ import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static org.springframework.util.ObjectUtils.isEmpty; import static org.springframework.util.ObjectUtils.isEmpty;
@ -34,6 +35,7 @@ import static ru.ulstu.paper.model.Paper.PaperStatus.ON_PREPARATION;
@Service @Service
public class PaperService { public class PaperService {
private final static int MAX_DISPLAY_SIZE = 40; private final static int MAX_DISPLAY_SIZE = 40;
private final static String PAPER_FORMATTED_TEMPLATE = "%s %s";
private final PaperNotificationService paperNotificationService; private final PaperNotificationService paperNotificationService;
private final PaperRepository paperRepository; private final PaperRepository paperRepository;
@ -66,13 +68,17 @@ public class PaperService {
return papers; return papers;
} }
public List<PaperDto> findAllActive() { public List<Paper> findAllActive() {
return findAllDto() return findAll()
.stream() .stream()
.filter(paper -> paper.getStatus() != COMPLETED && paper.getStatus() != FAILED) .filter(paper -> paper.getStatus() != COMPLETED && paper.getStatus() != FAILED)
.collect(toList()); .collect(toList());
} }
public List<PaperDto> findAllActiveDto() {
return convert(findAllActive(), PaperDto::new);
}
public PaperDto findOneDto(Integer id) { public PaperDto findOneDto(Integer id) {
return new PaperDto(paperRepository.findOne(id)); return new PaperDto(paperRepository.findOne(id));
} }
@ -212,4 +218,25 @@ public class PaperService {
public List<User> getPaperAuthors() { public List<User> getPaperAuthors() {
return userService.findAll(); return userService.findAll();
} }
public List<String> getFormattedPaperList() {
return findAllCompleted()
.stream()
.map(paper -> String.format(PAPER_FORMATTED_TEMPLATE, paper.getTitle(), getAuthors(paper)))
.collect(toList());
}
private List<Paper> findAllCompleted() {
return findAll()
.stream()
.filter(paper -> paper.getStatus() == COMPLETED)
.collect(toList());
}
private String getAuthors(Paper paper) {
return paper.getAuthors()
.stream()
.map(User::getUserAbbreviate)
.collect(Collectors.joining(", "));
}
} }

View File

@ -24,6 +24,8 @@ import java.util.Set;
@Entity @Entity
@Table(name = "users") @Table(name = "users")
public class User extends BaseEntity { public class User extends BaseEntity {
private final static String USER_ABBREVIATE_TEMPLATE = "%s %s%s";
@NotNull @NotNull
@Pattern(regexp = Constants.LOGIN_REGEX) @Pattern(regexp = Constants.LOGIN_REGEX)
@Size(min = 1, max = 50) @Size(min = 1, max = 50)
@ -186,4 +188,11 @@ public class User extends BaseEntity {
public void setPatronymic(String patronymic) { public void setPatronymic(String patronymic) {
this.patronymic = patronymic; this.patronymic = patronymic;
} }
public String getUserAbbreviate() {
return String.format(USER_ABBREVIATE_TEMPLATE,
lastName == null ? "" : lastName,
firstName == null ? "" : firstName.substring(0, 1) + ".",
patronymic == null ? "" : patronymic.substring(0, 1) + ".");
}
} }