Guest User

Untitled

a guest
May 25th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. public class PostServices : IPostServices
  2. {
  3. private const int PAGE_SIZE = 10;
  4. IPostRepository postRepository;
  5.  
  6. public IList<Post> Recent(int pageNumber)
  7. {
  8. return this.postRepository.GetNewest(pageNumber, PAGE_SIZE);
  9. }
  10.  
  11. public PostServices(IPostRepository postRepository)
  12. {
  13. this.postRepository = postRepository;
  14. }
  15.  
  16. private bool postDoesNotExist(string url)
  17. {
  18. return postRepository.GetByID(url) != null && postRepository.GetByID(url).ID != null;
  19. }
  20.  
  21.  
  22. private string getUniqueUrl(Post post)
  23. {
  24. string url = post.Subject.ToUrl();
  25.  
  26. if (postDoesNotExist(url))
  27. url += "-" + post.Owner.UserName;
  28.  
  29. while (postDoesNotExist(url))
  30. {
  31. url += "-1";
  32. }
  33. return url;
  34. }
  35.  
  36. public Post GetByID(string id)
  37. {
  38. return postRepository.GetByID(id);
  39. }
  40.  
  41. public void Save(Post post)
  42. {
  43. if (String.IsNullOrEmpty(post.ID))
  44. post.ID = getUniqueUrl(post);
  45. postRepository.Save(post);
  46. }
  47. }
Add Comment
Please, Sign In to add comment