Advertisement
altair0010

SubmitRating

Feb 4th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.72 KB | None | 0 0
  1. // POST: SubmitRating
  2. [HttpPost]
  3. public ActionResult SubmitRating(FormCollection form)
  4. {
  5. //Attempt to find ratedId.
  6. var existingRatedId = FindRated(form);
  7. rated rated;
  8. bool isNew = false;
  9.  
  10. using (var db = new Entities())
  11. {
  12. if (existingRatedId <= 0)
  13. {
  14. city currentCity = null;
  15. int? gender = null;
  16. int cityId;
  17. if (int.TryParse(form["city"], out cityId))
  18. currentCity = db.cities.FirstOrDefault(ct => ct.cityId == cityId);
  19. if (!form["gender"].IsEmpty())
  20. gender = int.Parse(form["gender"]);
  21. rated = new rated
  22. {
  23. firstName = form["first_name"],
  24. middleName = form["middle_name"],
  25. lastName = form["last_name"],
  26. cityId = currentCity?.cityId,
  27. gender = gender
  28. };
  29.  
  30. int age;
  31. if (int.TryParse(form["age"], out age))
  32. rated.dob = DateTime.Today - TimeSpan.FromDays(age*365.25);
  33.  
  34. if (existingRatedId < 0)
  35. rated.ratedMergeId = (int)(existingRatedId*-1);
  36.  
  37. rated = db.rateds.Add(rated);
  38. }
  39. else
  40. {
  41. rated = db.rateds.Find(existingRatedId);
  42. }
  43.  
  44. //Add contact info
  45. foreach (var ric in _ratingInitialContacts)
  46. {
  47. if (!String.IsNullOrEmpty(form["contact_" + ric.Key]))
  48. {
  49. var contact = new rated_contact()
  50. {
  51. rated = rated,
  52. contact_type = db.contact_type.First(x => x.name == ric.Value),
  53. value = form["contact_" + ric.Key]
  54. };
  55.  
  56. rated.rated_contact.Add(contact);
  57. }
  58. }
  59.  
  60. foreach (string key in form.Keys)
  61. {
  62. if (key.StartsWith("os_site"))
  63. {
  64. int cn = int.Parse(key.Substring(("os_site").Length));
  65.  
  66. string siteName = form["os_site" + cn];
  67. contact_type contactType = db.contact_type.FirstOrDefault(x => x.name == siteName);
  68.  
  69. if (contactType == null)
  70. {
  71. if (!form["os_site" + cn].IsEmpty())
  72. {
  73. //add it.
  74. contactType = new contact_type()
  75. {
  76. name = form["os_site" + cn]
  77. };
  78.  
  79. contactType = db.contact_type.Add(contactType);
  80. }
  81. }
  82.  
  83. if (contactType != null)
  84. {
  85. var contact = new rated_contact()
  86. {
  87. rated = rated,
  88. contact_type = contactType,
  89. value = form["os_username" + cn]
  90. };
  91.  
  92. rated.rated_contact.Add(contact);
  93. contactType.popularity++;
  94. }
  95. }
  96. }
  97.  
  98. int relId;
  99. relationship rel;
  100.  
  101. if (int.TryParse(form["relationship"], out relId))
  102. {
  103. rel = db.relationships.FirstOrDefault(x => x.relationshipId == relId);
  104. }
  105. else
  106. {
  107. if (!form["relationship_other"].IsEmpty())
  108. {
  109. rel = db.relationships.FirstOrDefault(x => x.name == form["relationship_other"]);
  110.  
  111. if (rel == null)
  112. {
  113. //create a new relationship
  114. rel = new relationship()
  115. {
  116. name = form["relationship_other"],
  117. hide = true
  118. };
  119. }
  120. }
  121. else
  122. rel = null;
  123. }
  124.  
  125. int parsed;
  126. int? rateOverall = null,
  127. rateNiceness = null,
  128. rateIntegrity = null,
  129. rateIntelligence = null,
  130. rateGenerosity = null,
  131. ratePerformace = null,
  132. rateAttractiveness = null,
  133. rateSex = null;
  134.  
  135. if (int.TryParse(form["rating_overall"], out parsed))
  136. rateOverall = parsed;
  137.  
  138. if (int.TryParse(form["rating_niceness"], out parsed))
  139. rateNiceness = parsed;
  140.  
  141. if (int.TryParse(form["rating_integrity"], out parsed))
  142. rateIntegrity = parsed;
  143.  
  144. if (int.TryParse(form["rating_intelligence"], out parsed))
  145. rateIntelligence = parsed;
  146.  
  147. if (int.TryParse(form["rating_generosity"], out parsed))
  148. rateGenerosity = parsed;
  149.  
  150. if (int.TryParse(form["rating_performance"], out parsed))
  151. ratePerformace = parsed;
  152.  
  153. if (int.TryParse(form["rating_attractiveness"], out parsed))
  154. rateAttractiveness = parsed;
  155.  
  156. if (int.TryParse(form["rating_sex"], out parsed))
  157. rateSex = parsed;
  158.  
  159. rater rater = null;
  160. string nickname = form["nickname"];
  161. if (User.Identity.IsAuthenticated)
  162. {
  163. int raterId;
  164. if (int.TryParse(User.Identity.Name, out raterId))
  165. {
  166. rater = db.raters.FirstOrDefault(x => x.raterId == raterId);
  167. if (rater != null)
  168. nickname = rater.nickname;
  169.  
  170. //update hero score.
  171. rater.lastPost = DateTime.Now;
  172. rater.rawHeroScore = rater.heroScore;
  173. rater.rawHeroScore += Math.Pow(Math.Atan(1 - (double)rater.rawHeroScore), 1.78) / 8.0;
  174. }
  175. }
  176.  
  177. var rating = new rated_rating()
  178. {
  179. nickname = nickname,
  180. relationship = rel,
  181. timestamp = DateTime.Now,
  182. review = form["review"],
  183. rated = rated,
  184. rater = rater,
  185. ratingOverallExperience = rateOverall,
  186. ratingNiceness = rateNiceness,
  187. ratingIntegrity = rateIntegrity,
  188. ratingIntelligence = rateIntelligence,
  189. ratingGenerosity = rateGenerosity,
  190. ratingPerformance = ratePerformace,
  191. ratingAttractiveness = rateAttractiveness,
  192. ratingSex = rateSex,
  193. hostAddress = Request.UserHostName
  194. };
  195.  
  196. db.rated_rating.Add(rating);
  197.  
  198. rated.averageOverallExperience = (int?)rated.rated_rating.Average(x => x.ratingOverallExperience);
  199. rated.averageNiceness = (int?)rated.rated_rating.Average(x => x.ratingNiceness);
  200. rated.averageIntegrity = (int?)rated.rated_rating.Average(x => x.ratingIntegrity);
  201. rated.averageIntelligence = (int?)rated.rated_rating.Average(x => x.ratingIntelligence);
  202. rated.averageGenerosity = (int?)rated.rated_rating.Average(x => x.ratingGenerosity);
  203. rated.averagePerformance = (int?)rated.rated_rating.Average(x => x.ratingPerformance);
  204. rated.averageAttractiveness = (int?)rated.rated_rating.Average(x => x.ratingAttractiveness);
  205. rated.averageSex = (int?)rated.rated_rating.Average(x => x.ratingSex);
  206. rated.totalReviews = rated.rated_rating.Count();
  207.  
  208. db.SaveChanges();
  209.  
  210. //Move picture(s) to appropriate directories.
  211. foreach (var picture in form["pictures"].Split(new [] {'|'}, StringSplitOptions.RemoveEmptyEntries))
  212. {
  213. var originalDirectory = new DirectoryInfo($"{Server.MapPath(@"\")}Images\\Temp");
  214. var destinationDirectory = new DirectoryInfo($"{Server.MapPath(@"\")}Images\\Pictures\\{rated.ratedId}");
  215.  
  216. if (!Directory.Exists(destinationDirectory.ToString()))
  217. Directory.CreateDirectory(destinationDirectory.ToString());
  218.  
  219. if (System.IO.File.Exists(Path.Combine(originalDirectory.ToString(), picture)))
  220. {
  221. System.IO.File.Move(Path.Combine(originalDirectory.ToString(), picture),
  222. Path.Combine(destinationDirectory.ToString(), picture));
  223.  
  224. var pic = db.pictures.Add(new picture()
  225. {
  226. filename = $"{rated.ratedId}/{picture}"
  227. });
  228.  
  229. rated.rated_picture.Add(new rated_picture()
  230. {
  231. rated = rated,
  232. picture = pic
  233. });
  234. }
  235. }
  236.  
  237. db.SaveChanges();
  238.  
  239. if (form.AllKeys.Contains("provider") && !form["provider"].IsEmpty())
  240. {
  241. return RedirectToAction("OAuthSignIn", "Accounts", new
  242. {
  243. provider = form["provider"],
  244. ratedRatingId = rating.ratedRatingId
  245. });
  246. }
  247. else
  248. {
  249. return RedirectToAction("RatedProfile", new { ratedId = rated.ratedId });
  250. }
  251. }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement