Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // POST: SubmitRating
- [HttpPost]
- public ActionResult SubmitRating(FormCollection form)
- {
- //Attempt to find ratedId.
- var existingRatedId = FindRated(form);
- rated rated;
- bool isNew = false;
- using (var db = new Entities())
- {
- if (existingRatedId <= 0)
- {
- city currentCity = null;
- int? gender = null;
- int cityId;
- if (int.TryParse(form["city"], out cityId))
- currentCity = db.cities.FirstOrDefault(ct => ct.cityId == cityId);
- if (!form["gender"].IsEmpty())
- gender = int.Parse(form["gender"]);
- rated = new rated
- {
- firstName = form["first_name"],
- middleName = form["middle_name"],
- lastName = form["last_name"],
- cityId = currentCity?.cityId,
- gender = gender
- };
- int age;
- if (int.TryParse(form["age"], out age))
- rated.dob = DateTime.Today - TimeSpan.FromDays(age*365.25);
- if (existingRatedId < 0)
- rated.ratedMergeId = (int)(existingRatedId*-1);
- rated = db.rateds.Add(rated);
- }
- else
- {
- rated = db.rateds.Find(existingRatedId);
- }
- //Add contact info
- foreach (var ric in _ratingInitialContacts)
- {
- if (!String.IsNullOrEmpty(form["contact_" + ric.Key]))
- {
- var contact = new rated_contact()
- {
- rated = rated,
- contact_type = db.contact_type.First(x => x.name == ric.Value),
- value = form["contact_" + ric.Key]
- };
- rated.rated_contact.Add(contact);
- }
- }
- foreach (string key in form.Keys)
- {
- if (key.StartsWith("os_site"))
- {
- int cn = int.Parse(key.Substring(("os_site").Length));
- string siteName = form["os_site" + cn];
- contact_type contactType = db.contact_type.FirstOrDefault(x => x.name == siteName);
- if (contactType == null)
- {
- if (!form["os_site" + cn].IsEmpty())
- {
- //add it.
- contactType = new contact_type()
- {
- name = form["os_site" + cn]
- };
- contactType = db.contact_type.Add(contactType);
- }
- }
- if (contactType != null)
- {
- var contact = new rated_contact()
- {
- rated = rated,
- contact_type = contactType,
- value = form["os_username" + cn]
- };
- rated.rated_contact.Add(contact);
- contactType.popularity++;
- }
- }
- }
- int relId;
- relationship rel;
- if (int.TryParse(form["relationship"], out relId))
- {
- rel = db.relationships.FirstOrDefault(x => x.relationshipId == relId);
- }
- else
- {
- if (!form["relationship_other"].IsEmpty())
- {
- rel = db.relationships.FirstOrDefault(x => x.name == form["relationship_other"]);
- if (rel == null)
- {
- //create a new relationship
- rel = new relationship()
- {
- name = form["relationship_other"],
- hide = true
- };
- }
- }
- else
- rel = null;
- }
- int parsed;
- int? rateOverall = null,
- rateNiceness = null,
- rateIntegrity = null,
- rateIntelligence = null,
- rateGenerosity = null,
- ratePerformace = null,
- rateAttractiveness = null,
- rateSex = null;
- if (int.TryParse(form["rating_overall"], out parsed))
- rateOverall = parsed;
- if (int.TryParse(form["rating_niceness"], out parsed))
- rateNiceness = parsed;
- if (int.TryParse(form["rating_integrity"], out parsed))
- rateIntegrity = parsed;
- if (int.TryParse(form["rating_intelligence"], out parsed))
- rateIntelligence = parsed;
- if (int.TryParse(form["rating_generosity"], out parsed))
- rateGenerosity = parsed;
- if (int.TryParse(form["rating_performance"], out parsed))
- ratePerformace = parsed;
- if (int.TryParse(form["rating_attractiveness"], out parsed))
- rateAttractiveness = parsed;
- if (int.TryParse(form["rating_sex"], out parsed))
- rateSex = parsed;
- rater rater = null;
- string nickname = form["nickname"];
- if (User.Identity.IsAuthenticated)
- {
- int raterId;
- if (int.TryParse(User.Identity.Name, out raterId))
- {
- rater = db.raters.FirstOrDefault(x => x.raterId == raterId);
- if (rater != null)
- nickname = rater.nickname;
- //update hero score.
- rater.lastPost = DateTime.Now;
- rater.rawHeroScore = rater.heroScore;
- rater.rawHeroScore += Math.Pow(Math.Atan(1 - (double)rater.rawHeroScore), 1.78) / 8.0;
- }
- }
- var rating = new rated_rating()
- {
- nickname = nickname,
- relationship = rel,
- timestamp = DateTime.Now,
- review = form["review"],
- rated = rated,
- rater = rater,
- ratingOverallExperience = rateOverall,
- ratingNiceness = rateNiceness,
- ratingIntegrity = rateIntegrity,
- ratingIntelligence = rateIntelligence,
- ratingGenerosity = rateGenerosity,
- ratingPerformance = ratePerformace,
- ratingAttractiveness = rateAttractiveness,
- ratingSex = rateSex,
- hostAddress = Request.UserHostName
- };
- db.rated_rating.Add(rating);
- rated.averageOverallExperience = (int?)rated.rated_rating.Average(x => x.ratingOverallExperience);
- rated.averageNiceness = (int?)rated.rated_rating.Average(x => x.ratingNiceness);
- rated.averageIntegrity = (int?)rated.rated_rating.Average(x => x.ratingIntegrity);
- rated.averageIntelligence = (int?)rated.rated_rating.Average(x => x.ratingIntelligence);
- rated.averageGenerosity = (int?)rated.rated_rating.Average(x => x.ratingGenerosity);
- rated.averagePerformance = (int?)rated.rated_rating.Average(x => x.ratingPerformance);
- rated.averageAttractiveness = (int?)rated.rated_rating.Average(x => x.ratingAttractiveness);
- rated.averageSex = (int?)rated.rated_rating.Average(x => x.ratingSex);
- rated.totalReviews = rated.rated_rating.Count();
- db.SaveChanges();
- //Move picture(s) to appropriate directories.
- foreach (var picture in form["pictures"].Split(new [] {'|'}, StringSplitOptions.RemoveEmptyEntries))
- {
- var originalDirectory = new DirectoryInfo($"{Server.MapPath(@"\")}Images\\Temp");
- var destinationDirectory = new DirectoryInfo($"{Server.MapPath(@"\")}Images\\Pictures\\{rated.ratedId}");
- if (!Directory.Exists(destinationDirectory.ToString()))
- Directory.CreateDirectory(destinationDirectory.ToString());
- if (System.IO.File.Exists(Path.Combine(originalDirectory.ToString(), picture)))
- {
- System.IO.File.Move(Path.Combine(originalDirectory.ToString(), picture),
- Path.Combine(destinationDirectory.ToString(), picture));
- var pic = db.pictures.Add(new picture()
- {
- filename = $"{rated.ratedId}/{picture}"
- });
- rated.rated_picture.Add(new rated_picture()
- {
- rated = rated,
- picture = pic
- });
- }
- }
- db.SaveChanges();
- if (form.AllKeys.Contains("provider") && !form["provider"].IsEmpty())
- {
- return RedirectToAction("OAuthSignIn", "Accounts", new
- {
- provider = form["provider"],
- ratedRatingId = rating.ratedRatingId
- });
- }
- else
- {
- return RedirectToAction("RatedProfile", new { ratedId = rated.ratedId });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement