コード例 #1
0
        public HttpResponseMessage GetAllPlaces(
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))]
            string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    PlacesContext context = new PlacesContext();

                    using (context)
                    {
                        UsersController.GetUserBySessionKey(context, sessionKey);
                        var placeModels = new HashSet<PlaceGetModel>();
                        foreach (var place in GetAllPlaces(context))
                        {
                            var model = new PlaceGetModel()
                            {
                                Id = place.Id,
                                Name = place.Name,
                                Town = new TownModel()
                                {
                                    Id = place.Town.Id,
                                    Name = place.Town.Name,
                                    CountryId = place.Town.Country.Id
                                },
                                Country = new CountrySmallModel()
                                {
                                    Id = place.Town.Country.Id,
                                    Name = place.Town.Country.Name
                                },
                                Content = place.Content,
                                ImageUrls = place.Images.Select(i => i.Url),
                                Comments = (from comment in place.Comments
                                            select new CommentModel()
                                            {
                                                Content = comment.Content,
                                                Date = comment.Date,
                                                PostedBy = comment.User.Nickname
                                            })
                            };
                            placeModels.Add(model);
                        }

                        return this.Request.CreateResponse(HttpStatusCode.OK, placeModels);
                    }
                });

            return responseMsg;
        }
コード例 #2
0
        public HttpResponseMessage GetPlaceById(
            int placeId,
            [ValueProvider(typeof(HeaderValueProviderFactory<string>))]
            string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    PlacesContext context = new PlacesContext();

                    using (context)
                    {
                        UsersController.GetUserBySessionKey(context, sessionKey);
                        var place = GetAllPlaces(context).FirstOrDefault(p => p.Id == placeId);
                        if (place == null)
                        {
                            throw new ArgumentNullException("place", "A place with the specified ID does not exist.");
                        }
                        var model = new PlaceGetModel()
                        {
                            Id = place.Id,
                            Name = place.Name,
                            Town = new TownModel()
                            {
                                Id = place.Town.Id,
                                Name = place.Town.Name,
                                CountryId = place.Town.Country.Id
                            },
                            Country = new CountrySmallModel()
                            {
                                Id = place.Town.Country.Id,
                                Name = place.Town.Country.Name
                            },
                            Content = place.Content,
                            ImageUrls = place.Images.Select(i => i.Url),
                            Comments = (from comment in place.Comments
                                        select new CommentModel()
                                        {
                                            Content = comment.Content,
                                            Date = comment.Date,
                                            PostedBy = comment.User.Nickname
                                        }).OrderByDescending(c => c.Date)
                        };

                        return this.Request.CreateResponse(HttpStatusCode.OK, model);
                    }
                });

            return responseMsg;
        }