Esempio n. 1
0
        public async Task <dynamic> GetByAuthorId(string id)
        {
            var adCollection = MongoWrapper.Database.GetCollection <Models.Advertisement>(nameof(Models.Advertisement));

            var AdvertisementFilterBuilder = new FilterDefinitionBuilder <Models.Advertisement>();
            var AdvertisementFilter        = AdvertisementFilterBuilder.And
                                             (
                AdvertisementFilterBuilder.Eq(p => p.Poster._id, new ObjectId(id)),
                GeneralUtils.NotDeactivated(AdvertisementFilterBuilder)
                                             );

            var AdvertisementSortBuilder = new SortDefinitionBuilder <Models.Advertisement>();
            var AdvertisementSort        = AdvertisementSortBuilder.Descending(p => p._id);

            var Advertisements = (await adCollection.FindAsync(AdvertisementFilter, new FindOptions <Models.Advertisement>
            {
                AllowPartialResults = true,
                Sort = AdvertisementSort
            })).ToList();

            if (Advertisements.Count > 0)
            {
                Models.User user = await RetrieveAuthor(Advertisements.First());

                Advertisements.ForEach(p => EnrichAdvertisementWithAuthor(p, user));

                return(new ResponseBody
                {
                    Code = ResponseCode.GenericSuccess,
                    Success = true,
                    Message = "Advertisements encontrados com sucesso!",
                    Data = Advertisements.Select(Advertisement => Advertisement.BuildAdResponse()),
                });
            }
            else
            {
                return(new ResponseBody
                {
                    Code = ResponseCode.GenericSuccess,
                    Success = true,
                    Message = "Nenhum Advertisement encontrado!",
                    Data = Array.Empty <Models.Advertisement>(),
                });
            }
        }