Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. package ru.itmo.webmail.model.service;
  2.  
  3. import ru.itmo.webmail.model.domain.Talk;
  4. import ru.itmo.webmail.model.exception.ValidationException;
  5. import ru.itmo.webmail.model.repository.TalkRepository;
  6. import ru.itmo.webmail.model.repository.impl.TalkRepositoryImpl;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.Date;
  10. import java.util.List;
  11.  
  12. public class TalkService {
  13.     private UserService userService = new UserService();
  14.     private TalkRepository talkRepository = new TalkRepositoryImpl();
  15.  
  16.     public void validateSending(String login, String text) throws ValidationException {
  17.         if (userService.findByLoginOrEmail(login) == null) {
  18.             throw new ValidationException("There is no user with such login or email");
  19.         }
  20.  
  21.         if (text == null || text.isEmpty()) {
  22.             throw new ValidationException("You have to write something");
  23.         }
  24.     }
  25.  
  26.     public void send(Long source, Long target, String text) {
  27.         talkRepository.save(new Talk(source, target, text));
  28.     }
  29.  
  30.     public List<Talk> findAllForUser(Long id) {
  31.         return talkRepository.findAllForUser(id);
  32.     }
  33.  
  34.     public List<FrontTalk> findAllFrontForUser(Long id) {
  35.         List<Talk> talks = findAllForUser(id);
  36.         List<FrontTalk> result = new ArrayList<>();
  37.         for (Talk talk: talks) {
  38.             result.add(new FrontTalk(userService.findById(talk.getSourceUserId()).getLogin(),
  39.                     userService.findById(talk.getTargetUserId()).getLogin(), talk.getText(),
  40.                     talk.getCreationTime()));
  41.         }
  42.         return result;
  43.     }
  44.  
  45.     public static class FrontTalk {
  46.         private String source;
  47.         private String target;
  48.         private String text;
  49.         private Date time;
  50.  
  51.         public Date getTime() {
  52.             return time;
  53.         }
  54.  
  55.         public void setTime(Date time) {
  56.             this.time = time;
  57.         }
  58.  
  59.         public FrontTalk(String source, String target, String text, Date time) {
  60.             this.target = target;
  61.             this.source = source;
  62.             this.text = text;
  63.             this.time = time;
  64.         }
  65.  
  66.  
  67.         public String getText() {
  68.             return text;
  69.         }
  70.  
  71.         public void setText(String text) {
  72.             this.text = text;
  73.         }
  74.  
  75.         public String getSource() {
  76.             return source;
  77.         }
  78.  
  79.         public void setSource(String source) {
  80.             this.source = source;
  81.         }
  82.  
  83.         public String getTarget() {
  84.             return target;
  85.         }
  86.  
  87.         public void setTarget(String target) {
  88.             this.target = target;
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement