/// <summary> /// Creates the interest in building from the user to the current /// listing. /// </summary> /// <param name="username">The username.</param> /// <param name="listingId">The listing id.</param> /// <returns> /// A status of whether or not the interest was created. /// </returns> public Status <UserInterest> CreateInterestInBuilding(string username, long listingId, string message) { if (listingId == 0) { return(Status.ValidationError <UserInterest>(null, "listingId", "listingId is required")); } if (string.IsNullOrWhiteSpace(username)) { return(Status.ValidationError <UserInterest>(null, "username", "username is required")); } using (var context = new RentlerContext()) { try { User user = (from u in context.Users where u.Username == username select u).SingleOrDefault(); if (user == null) { return(Status.NotFound <UserInterest>()); } UserInterest newInterest = new UserInterest() { BuildingId = listingId, UserId = user.UserId, Message = message }; context.UserInterests.Add(newInterest); context.SaveChanges(); // loads the building, which generated this interest, into newInterest context.Entry(newInterest).Reference(e => e.Building).Load(); // notify landlord of interest // TODO: if we are unable to send this email we need a way to allow the user // to re-notify the Landlord without requiring them to continue to create // interests EmailListingInterestedModel model = new EmailListingInterestedModel(newInterest); mailer.Interested(model); return(Status.OK <UserInterest>(newInterest)); } catch (Exception ex) { // log exception return(Status.Error <UserInterest>("System was unable to create interest", null)); } } }
public ActionResult Interested(EmailListingInterestedModel model) { return(Json(mailer.Interested(model))); }