Advertisement
Guest User

Untitled

a guest
Mar 11th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.60 KB | None | 0 0
  1. amespace PhotoShare.Services
  2. {
  3. using System;
  4. using System.Linq;
  5.  
  6. using AutoMapper.QueryableExtensions;
  7.  
  8. using Contracts;
  9. using PhotoShare.Models;
  10. using PhotoShare.Data;
  11.  
  12. public class UserService : IUserService
  13. {
  14. private PhotoShareContext context;
  15. public UserService(PhotoShareContext photoShareContext)
  16. {
  17. this.context = photoShareContext;
  18. }
  19. public Friendship AcceptFriend(int userId, int friendId)
  20. {
  21. bool areAlreadyFriends = this.context.Friendships.Any(x => x.FriendId == userId &&x.UserId ==friendId) && this.context.Friendships.Any(x => x.UserId == userId && x.FriendId ==friendId) == true;
  22. var usernameReciever = ById<User>(userId).Username;
  23. var usernameSender = ById<User>(friendId).Username;
  24. if (areAlreadyFriends)
  25. {
  26. throw new InvalidOperationException($"{usernameReciever} is already a friend to {usernameSender}");
  27. }
  28. if (!this.context.Friendships.Any(x=>x.UserId == friendId && x.FriendId==userId))
  29. {
  30. throw new InvalidOperationException($"{usernameSender} has not added {usernameReciever} as a friend");
  31. }
  32.  
  33. var friendship = new Friendship() { UserId = userId, FriendId = friendId };
  34. this.context.Friendships.Add(friendship);
  35. this.context.SaveChanges();
  36. return friendship;
  37. }
  38.  
  39. public Friendship AddFriend(int userId, int friendId)
  40. {
  41. Friendship friendship = new Friendship() { UserId = userId, FriendId = friendId };
  42.  
  43. this.context.Friendships.Add(friendship);
  44. this.context.SaveChanges();
  45. return friendship;
  46. }
  47. public TModel ByUsernameAndPassword<TModel>(string username, string password)
  48. {
  49. TModel model = this.context.Users
  50. .Where(x => x.Username == username && x.Password == password)
  51. .ProjectTo<TModel>()
  52. .FirstOrDefault();
  53. return model;
  54. }
  55. public TModel ById<TModel>(int id)
  56. {
  57. TModel model = this.context.Users
  58. .Where(t => t.Id == id)
  59. .ProjectTo<TModel>().First();
  60. return model;
  61. }
  62.  
  63. public TModel ByUsername<TModel>(string username)
  64. {
  65. TModel userDto = this.context.Users
  66. .Where(e => e.Username == username)
  67. .ProjectTo<TModel>()
  68. .SingleOrDefault();
  69.  
  70. return userDto;
  71. }
  72.  
  73. public void ChangePassword(int userId, string password)
  74. {
  75. var user = this.context.Users.Find(userId);
  76. user.Password = password;
  77. this.context.Users.Update(user);
  78. this.context.SaveChanges();
  79. }
  80.  
  81. public void Delete(string username)
  82. {
  83. var user = this.context.Users
  84. .Where(e => e.Username == username)
  85. .FirstOrDefault();
  86. user.IsDeleted = true;
  87.  
  88. this.context.Users.Update(user);
  89. this.context.SaveChanges();
  90. }
  91.  
  92. public bool Exists(int id)
  93. {
  94. var user = context.Users.Any(e => e.Id == id);
  95. if (user)
  96. {
  97. return true;
  98. }
  99. return false;
  100. }
  101.  
  102. public bool Exists(string name)
  103. {
  104. var user = context.Users.Any(e => e.Username == name);
  105. if (user)
  106. {
  107. return true;
  108. }
  109. return false;
  110. }
  111.  
  112. public string Register(string username, string password, string email)
  113. {
  114. var user = new User()
  115. {
  116. Username = username,
  117. Password = password,
  118. Email = email
  119. };
  120. context.Users.Add(user);
  121. this.context.SaveChanges();
  122. return $"User {username} was registered successfully";
  123. }
  124.  
  125. public void SetBornTown(int userId, int townId)
  126. {
  127. var user = this.context.Users.Find(userId);
  128. user.BornTownId = townId;
  129. this.context.Users.Update(user);
  130. this.context.SaveChanges();
  131. }
  132.  
  133. public void SetCurrentTown(int userId, int townId)
  134. {
  135. var user = this.context.Users.Find(userId);
  136. user.CurrentTownId = townId;
  137. this.context.Users.Update(user);
  138. this.context.SaveChanges();
  139. }
  140.  
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement