Created
May 1, 2012 13:29
-
-
Save jchannon/2567906 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.ComponentModel.DataAnnotations; | |
| using System.Linq; | |
| using Newtonsoft.Json; | |
| namespace DinnerParty.Models | |
| { | |
| public class Dinner | |
| { | |
| /// <summary> | |
| /// Id (in that exact case) is used by Raven. | |
| /// If on a Url of /dinners/edit/162 Nancy will bind the Id property to 162 so, | |
| /// we have to make sure that it starts with dinners/ so Raven can identify it properly | |
| /// </summary> | |
| private string id; | |
| public string Id | |
| { | |
| get { return id; } | |
| set | |
| { | |
| if (!value.StartsWith("dinners")) | |
| value = "dinners/" + value; | |
| id = value; | |
| } | |
| } | |
| //[HiddenInput(DisplayValue = false)] | |
| [JsonIgnore] | |
| public int DinnerID | |
| { | |
| get | |
| { | |
| return int.Parse(Id.Substring(Id.LastIndexOf("/") + 1)); | |
| } | |
| } | |
| [Required(ErrorMessage = "Title is required")] | |
| [StringLength(50, ErrorMessage = "Title may not be longer than 50 characters")] | |
| public string Title { get; set; } | |
| [Required(ErrorMessage = "Event Date is required")] | |
| [Display(Name = "Event Date")] | |
| public DateTime EventDate { get; set; } | |
| [Required(ErrorMessage = "Description is required")] | |
| [StringLength(256, ErrorMessage = "Description may not be longer than 256 characters")] | |
| [DataType(DataType.MultilineText)] | |
| public string Description { get; set; } | |
| [StringLength(20, ErrorMessage = "Hosted By name may not be longer than 20 characters")] | |
| [Display(Name = "Host's Name")] | |
| public string HostedBy { get; set; } | |
| [Required(ErrorMessage = "Contact info is required")] | |
| [StringLength(20, ErrorMessage = "Contact info may not be longer than 20 characters")] | |
| [Display(Name = "Contact Info")] | |
| public string ContactPhone { get; set; } | |
| [Required(ErrorMessage = "Address is required")] | |
| [StringLength(50, ErrorMessage = "Address may not be longer than 50 characters")] | |
| [Display(Name = "Address, City, State, ZIP")] | |
| public string Address { get; set; } | |
| [UIHint("CountryDropDown")] | |
| public string Country { get; set; } | |
| //[HiddenInput(DisplayValue = false)] | |
| public double Latitude { get; set; } | |
| //[HiddenInput(DisplayValue = false)] | |
| public double Longitude { get; set; } | |
| //[HiddenInput(DisplayValue = false)] | |
| public string HostedById { get; set; } | |
| public virtual ICollection<RSVP> RSVPs { get; set; } | |
| public bool IsHostedBy(string userName) | |
| { | |
| return String.Equals(HostedById ?? HostedBy, userName, StringComparison.Ordinal); | |
| } | |
| public bool IsUserRegistered(string userName) | |
| { | |
| return RSVPs.Any(r => r.AttendeeNameId == userName || (r.AttendeeNameId == null && r.AttendeeName == userName)); | |
| } | |
| [UIHint("LocationDetail")] | |
| [JsonIgnore] | |
| public LocationDetail Location | |
| { | |
| get | |
| { | |
| return new LocationDetail() { Latitude = this.Latitude, Longitude = this.Longitude, Title = this.Title, Address = this.Address }; | |
| } | |
| set | |
| { | |
| this.Latitude = value.Latitude; | |
| this.Longitude = value.Longitude; | |
| this.Title = value.Title; | |
| this.Address = value.Address; | |
| } | |
| } | |
| } | |
| public class LocationDetail | |
| { | |
| public double Latitude; | |
| public double Longitude; | |
| public string Title; | |
| public string Address; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class JsonDinner | |
| { | |
| public int DinnerID { get; set; } | |
| public DateTime EventDate { get; set; } | |
| public string Title { get; set; } | |
| public double Latitude { get; set; } | |
| public double Longitude { get; set; } | |
| public string Description { get; set; } | |
| public int RSVPCount { get; set; } | |
| public string Url { get; set; } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Default the limit to 40, if not supplied. | |
| if (!parameters.limit.HasValue || String.IsNullOrWhiteSpace(parameters.limit)) | |
| parameters.limit = 40; | |
| var dinners = DocumentSession.Query<Dinner>() | |
| .Where(x => x.EventDate >= DateTime.Now) | |
| .Take((int)parameters.limit) | |
| .OrderByDescending(x => x.RSVPs.Count); | |
| var jsonDinners = | |
| dinners.AsEnumerable() | |
| .Select(item => JsonDinnerFromDinner(item)); | |
| return Response.AsJson(jsonDinners.ToList()); | |
| private JsonDinner JsonDinnerFromDinner(Dinner dinner) | |
| { | |
| return new JsonDinner | |
| { | |
| DinnerID = dinner.DinnerID, | |
| EventDate = dinner.EventDate, | |
| Latitude = dinner.Latitude, | |
| Longitude = dinner.Longitude, | |
| Title = dinner.Title, | |
| Description = dinner.Description, | |
| RSVPCount = dinner.RSVPs.Count, | |
| //TODO: Need to mock this out for testing... | |
| //Url = Url.RouteUrl("PrettyDetails", new { Id = dinner.DinnerID } ) | |
| Url = dinner.DinnerID.ToString() | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment