Exemplo n.º 1
0
        public async Task <IActionResult> CreateTravelRoute([FromBody] TravelRouteForCreationDto travelRouteForCreationDto)
        {
            var travelRouteModel = _mapper.Map <TravelRoute>(travelRouteForCreationDto);

            _travelRouteRepository.AddTravelRoute(travelRouteModel);
            await _travelRouteRepository.SaveAsync();

            //把新增的資料map出來變成TravelRouteDto傳回給API作為資料輸出。
            var travelRouteToReturn = _mapper.Map <TravelRouteDto>(travelRouteModel);

            //這邊除了回傳mapping後的travelRouteToReturn資料,也會在回傳的Header裡夾帶使用Get請求的GetTravelRouteById路由+這個新增項目的id
            //就是一個完整的取得此新增項目的GET請求的url:
            //https://localhost:5001/api/TravelRoutes?travelRouteId=889f0a7b-55c8-4de2-926f-910a00c45cbe
            return(CreatedAtRoute("GetTravelRouteById", new { travelRouteId = travelRouteToReturn.Id }, travelRouteToReturn));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> CreateTravelRoutePicture([FromRoute] Guid travelRouteId, [FromBody] TravelRoutePictureForCreationDto travelRoutePictureForCreationDto)
        {
            if (!(await _travelRouteRepository.TravelRouteExistsAsync(travelRouteId)))
            {
                return(NotFound("旅遊路線不存在"));
            }
            var pictureModel = _mapper.Map <TravelRoutePicture>(travelRoutePictureForCreationDto);

            _travelRouteRepository.AddTravelRoutePicture(travelRouteId, pictureModel);
            await _travelRouteRepository.SaveAsync();

            var pictureToReturn = _mapper.Map <TravelRoutePictureDto>(pictureModel);

            return(CreatedAtRoute("GetPicture", new { travelRouteId = pictureModel.TravelRouteId, pictureId = pictureModel.Id }, pictureToReturn));
        }