|
| 1 | +usingSystem; |
| 2 | +usingSystem.Linq; |
| 3 | +usingAutoMapper; |
| 4 | +usingSampleWebApiAspNetCore.Dtos; |
| 5 | +usingMicrosoft.AspNetCore.JsonPatch; |
| 6 | +usingMicrosoft.AspNetCore.Mvc; |
| 7 | +usingSampleWebApiAspNetCore.Repositories; |
| 8 | +usingSystem.Collections.Generic; |
| 9 | +usingSampleWebApiAspNetCore.Entities; |
| 10 | +usingSampleWebApiAspNetCore.Models; |
| 11 | +usingSampleWebApiAspNetCore.Helpers; |
| 12 | +usingMicrosoft.AspNetCore.Authorization; |
| 13 | + |
| 14 | +namespaceSampleWebApiAspNetCore.Controllers |
| 15 | +{ |
| 16 | +[ApiVersion("1.0")] |
| 17 | +[Route("api/v{version:apiVersion}/[controller]")] |
| 18 | +// [Route("api/[controller]")] |
| 19 | +publicclassFoodsController:Controller |
| 20 | +{ |
| 21 | +privatereadonlyIFoodRepository_foodRepository; |
| 22 | +privatereadonlyIUrlHelper_urlHelper; |
| 23 | + |
| 24 | +publicFoodsController(IUrlHelperurlHelper,IFoodRepositoryfoodRepository) |
| 25 | +{ |
| 26 | +_foodRepository=foodRepository; |
| 27 | +_urlHelper=urlHelper; |
| 28 | +} |
| 29 | + |
| 30 | +[HttpGet(Name=nameof(GetAllFoods))] |
| 31 | +publicIActionResultGetAllFoods([FromQuery]QueryParametersqueryParameters) |
| 32 | +{ |
| 33 | +List<FoodItem>foodItems=_foodRepository.GetAll(queryParameters).ToList(); |
| 34 | + |
| 35 | +varallItemCount=_foodRepository.Count(); |
| 36 | + |
| 37 | +varpaginationMetadata=new |
| 38 | +{ |
| 39 | +totalCount=allItemCount, |
| 40 | +pageSize=queryParameters.PageCount, |
| 41 | +currentPage=queryParameters.Page, |
| 42 | +totalPages=queryParameters.GetTotalPages(allItemCount) |
| 43 | +}; |
| 44 | + |
| 45 | +Response.Headers.Add("X-Pagination", |
| 46 | +Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata)); |
| 47 | + |
| 48 | +varlinks=CreateLinksForCollection(queryParameters,allItemCount); |
| 49 | + |
| 50 | +vartoReturn=foodItems.Select(x=>ExpandSingleFoodItem(x)); |
| 51 | + |
| 52 | +returnOk(new |
| 53 | +{ |
| 54 | +value=toReturn, |
| 55 | +links=links |
| 56 | +}); |
| 57 | +} |
| 58 | + |
| 59 | +[HttpGet] |
| 60 | +[Route("{id:int}",Name=nameof(GetSingleFood))] |
| 61 | +publicIActionResultGetSingleFood(intid) |
| 62 | +{ |
| 63 | +FoodItemfoodItem=_foodRepository.GetSingle(id); |
| 64 | + |
| 65 | +if(foodItem==null) |
| 66 | +{ |
| 67 | +returnNotFound(); |
| 68 | +} |
| 69 | + |
| 70 | +returnOk(ExpandSingleFoodItem(foodItem)); |
| 71 | +} |
| 72 | + |
| 73 | +[HttpPost(Name=nameof(AddFood))] |
| 74 | +publicIActionResultAddFood([FromBody]FoodCreateDtofoodCreateDto) |
| 75 | +{ |
| 76 | +if(foodCreateDto==null) |
| 77 | +{ |
| 78 | +returnBadRequest(); |
| 79 | +} |
| 80 | + |
| 81 | +if(!ModelState.IsValid) |
| 82 | +{ |
| 83 | +returnBadRequest(ModelState); |
| 84 | +} |
| 85 | + |
| 86 | +FoodItemtoAdd=Mapper.Map<FoodItem>(foodCreateDto); |
| 87 | + |
| 88 | +_foodRepository.Add(toAdd); |
| 89 | + |
| 90 | +if(!_foodRepository.Save()) |
| 91 | +{ |
| 92 | +thrownewException("Creating a fooditem failed on save."); |
| 93 | +} |
| 94 | + |
| 95 | +FoodItemnewFoodItem=_foodRepository.GetSingle(toAdd.Id); |
| 96 | + |
| 97 | +returnCreatedAtRoute(nameof(GetSingleFood),new{id=newFoodItem.Id}, |
| 98 | +Mapper.Map<FoodItemDto>(newFoodItem)); |
| 99 | +} |
| 100 | + |
| 101 | +[HttpPatch("{id:int}",Name=nameof(PartiallyUpdateFood))] |
| 102 | +publicIActionResultPartiallyUpdateFood(intid,[FromBody]JsonPatchDocument<FoodUpdateDto>patchDoc) |
| 103 | +{ |
| 104 | +if(patchDoc==null) |
| 105 | +{ |
| 106 | +returnBadRequest(); |
| 107 | +} |
| 108 | + |
| 109 | +FoodItemexistingEntity=_foodRepository.GetSingle(id); |
| 110 | + |
| 111 | +if(existingEntity==null) |
| 112 | +{ |
| 113 | +returnNotFound(); |
| 114 | +} |
| 115 | + |
| 116 | +FoodUpdateDtofoodUpdateDto=Mapper.Map<FoodUpdateDto>(existingEntity); |
| 117 | +patchDoc.ApplyTo(foodUpdateDto,ModelState); |
| 118 | + |
| 119 | +TryValidateModel(foodUpdateDto); |
| 120 | + |
| 121 | +if(!ModelState.IsValid) |
| 122 | +{ |
| 123 | +returnBadRequest(ModelState); |
| 124 | +} |
| 125 | + |
| 126 | +Mapper.Map(foodUpdateDto,existingEntity); |
| 127 | +FoodItemupdated=_foodRepository.Update(id,existingEntity); |
| 128 | + |
| 129 | +if(!_foodRepository.Save()) |
| 130 | +{ |
| 131 | +thrownewException("Updating a fooditem failed on save."); |
| 132 | +} |
| 133 | + |
| 134 | +returnOk(Mapper.Map<FoodItemDto>(updated)); |
| 135 | +} |
| 136 | + |
| 137 | +[HttpDelete] |
| 138 | +[Route("{id:int}",Name=nameof(RemoveFood))] |
| 139 | +publicIActionResultRemoveFood(intid) |
| 140 | +{ |
| 141 | +FoodItemfoodItem=_foodRepository.GetSingle(id); |
| 142 | + |
| 143 | +if(foodItem==null) |
| 144 | +{ |
| 145 | +returnNotFound(); |
| 146 | +} |
| 147 | + |
| 148 | +_foodRepository.Delete(id); |
| 149 | + |
| 150 | +if(!_foodRepository.Save()) |
| 151 | +{ |
| 152 | +thrownewException("Deleting a fooditem failed on save."); |
| 153 | +} |
| 154 | + |
| 155 | +returnNoContent(); |
| 156 | +} |
| 157 | + |
| 158 | +[HttpPut] |
| 159 | +[Route("{id:int}",Name=nameof(UpdateFood))] |
| 160 | +publicIActionResultUpdateFood(intid,[FromBody]FoodUpdateDtofoodUpdateDto) |
| 161 | +{ |
| 162 | +if(foodUpdateDto==null) |
| 163 | +{ |
| 164 | +returnBadRequest(); |
| 165 | +} |
| 166 | + |
| 167 | +varexistingFoodItem=_foodRepository.GetSingle(id); |
| 168 | + |
| 169 | +if(existingFoodItem==null) |
| 170 | +{ |
| 171 | +returnNotFound(); |
| 172 | +} |
| 173 | + |
| 174 | +if(!ModelState.IsValid) |
| 175 | +{ |
| 176 | +returnBadRequest(ModelState); |
| 177 | +} |
| 178 | + |
| 179 | +Mapper.Map(foodUpdateDto,existingFoodItem); |
| 180 | + |
| 181 | +_foodRepository.Update(id,existingFoodItem); |
| 182 | + |
| 183 | +if(!_foodRepository.Save()) |
| 184 | +{ |
| 185 | +thrownewException("Updating a fooditem failed on save."); |
| 186 | +} |
| 187 | + |
| 188 | +returnOk(Mapper.Map<FoodItemDto>(existingFoodItem)); |
| 189 | +} |
| 190 | + |
| 191 | +[HttpGet("GetRandomMeal",Name=nameof(GetRandomMeal))] |
| 192 | +publicIActionResultGetRandomMeal() |
| 193 | +{ |
| 194 | +ICollection<FoodItem>foodItems=_foodRepository.GetRandomMeal(); |
| 195 | + |
| 196 | +IEnumerable<FoodItemDto>dtos=foodItems |
| 197 | +.Select(x=>Mapper.Map<FoodItemDto>(x)); |
| 198 | + |
| 199 | +varlinks=newList<LinkDto>(); |
| 200 | + |
| 201 | +// self |
| 202 | +links.Add(newLinkDto(_urlHelper.Link(nameof(GetRandomMeal),null),"self","GET")); |
| 203 | + |
| 204 | +returnOk(new |
| 205 | +{ |
| 206 | +value=dtos, |
| 207 | +links=links |
| 208 | +}); |
| 209 | +} |
| 210 | + |
| 211 | +privateList<LinkDto>CreateLinksForCollection(QueryParametersqueryParameters,inttotalCount) |
| 212 | +{ |
| 213 | +varlinks=newList<LinkDto>(); |
| 214 | + |
| 215 | +// self |
| 216 | +links.Add( |
| 217 | +newLinkDto(_urlHelper.Link(nameof(GetAllFoods),new |
| 218 | +{ |
| 219 | +pagecount=queryParameters.PageCount, |
| 220 | +page=queryParameters.Page, |
| 221 | +orderby=queryParameters.OrderBy |
| 222 | +}),"self","GET")); |
| 223 | + |
| 224 | +links.Add(newLinkDto(_urlHelper.Link(nameof(GetAllFoods),new |
| 225 | +{ |
| 226 | +pagecount=queryParameters.PageCount, |
| 227 | +page=1, |
| 228 | +orderby=queryParameters.OrderBy |
| 229 | +}),"first","GET")); |
| 230 | + |
| 231 | +links.Add(newLinkDto(_urlHelper.Link(nameof(GetAllFoods),new |
| 232 | +{ |
| 233 | +pagecount=queryParameters.PageCount, |
| 234 | +page=queryParameters.GetTotalPages(totalCount), |
| 235 | +orderby=queryParameters.OrderBy |
| 236 | +}),"last","GET")); |
| 237 | + |
| 238 | +if(queryParameters.HasNext(totalCount)) |
| 239 | +{ |
| 240 | +links.Add(newLinkDto(_urlHelper.Link(nameof(GetAllFoods),new |
| 241 | +{ |
| 242 | +pagecount=queryParameters.PageCount, |
| 243 | +page=queryParameters.Page+1, |
| 244 | +orderby=queryParameters.OrderBy |
| 245 | +}),"next","GET")); |
| 246 | +} |
| 247 | + |
| 248 | +if(queryParameters.HasPrevious()) |
| 249 | +{ |
| 250 | +links.Add(newLinkDto(_urlHelper.Link(nameof(GetAllFoods),new |
| 251 | +{ |
| 252 | +pagecount=queryParameters.PageCount, |
| 253 | +page=queryParameters.Page-1, |
| 254 | +orderby=queryParameters.OrderBy |
| 255 | +}),"previous","GET")); |
| 256 | +} |
| 257 | + |
| 258 | +returnlinks; |
| 259 | +} |
| 260 | + |
| 261 | +privatedynamicExpandSingleFoodItem(FoodItemfoodItem) |
| 262 | +{ |
| 263 | +varlinks=GetLinks(foodItem.Id); |
| 264 | +FoodItemDtoitem=Mapper.Map<FoodItemDto>(foodItem); |
| 265 | + |
| 266 | +varresourceToReturn=item.ToDynamic()asIDictionary<string,object>; |
| 267 | +resourceToReturn.Add("links",links); |
| 268 | + |
| 269 | +returnresourceToReturn; |
| 270 | +} |
| 271 | + |
| 272 | +privateIEnumerable<LinkDto>GetLinks(intid) |
| 273 | +{ |
| 274 | +varlinks=newList<LinkDto>(); |
| 275 | + |
| 276 | +links.Add( |
| 277 | +newLinkDto(_urlHelper.Link(nameof(GetSingleFood),new{id=id}), |
| 278 | +"self", |
| 279 | +"GET")); |
| 280 | + |
| 281 | +links.Add( |
| 282 | +newLinkDto(_urlHelper.Link(nameof(RemoveFood),new{id=id}), |
| 283 | +"delete_food", |
| 284 | +"DELETE")); |
| 285 | + |
| 286 | +links.Add( |
| 287 | +newLinkDto(_urlHelper.Link(nameof(AddFood),null), |
| 288 | +"create_food", |
| 289 | +"POST")); |
| 290 | + |
| 291 | +links.Add( |
| 292 | +newLinkDto(_urlHelper.Link(nameof(UpdateFood),new{id=id}), |
| 293 | +"update_food", |
| 294 | +"PUT")); |
| 295 | + |
| 296 | +returnlinks; |
| 297 | +} |
| 298 | +} |
| 299 | + |
| 300 | +[ApiVersion("2.0")] |
| 301 | +[Route("api/v{version:apiVersion}/foods")] |
| 302 | +publicclassFoods2Controller:Controller |
| 303 | +{ |
| 304 | +[HttpGet] |
| 305 | +publicIActionResultGet() |
| 306 | +{ |
| 307 | +returnOk("2.0"); |
| 308 | +} |
| 309 | +} |
| 310 | +} |