Beispiel #1
0
        public async Task <IActionResult> UpdateHallByIdAsync(
            [FromRoute, SwaggerParameter(Description = "The id of hall to update", Required = true)] Guid hallId,
            [FromBody, SwaggerParameter(Description = "Hall to update", Required = true)] HallToUpdateDto hallToUpdate,
            [FromHeader(Name = "Accept"), SwaggerParameter(Description = "media type to request betwen json or json+hateoas")] string mediaType)
        {
            var hallFromDb = await _hallRepository.GetHallAsync(hallId);

            // upserting if movie does not already exist
            if (hallFromDb == null)
            {
                var hallEntity = Mapper.Map <Hall>(hallToUpdate);
                hallEntity.Id = hallId;
                _hallRepository.AddHall(hallEntity);

                if (!await _hallRepository.SaveChangesAsync())
                {
                    _logger.LogError($"Upserting hall: {hallId} failed on save");
                }

                var hallToReturn = Mapper.Map <HallDto>(hallEntity);

                if (mediaType == "application/vnd.biob.json+hateoas")
                {
                    var links = CreateLinksForHall(hallToReturn.Id);

                    var linkedHall = hallToReturn.ShapeData(null) as IDictionary <string, object>;

                    linkedHall.Add("links", links);

                    return(CreatedAtRoute("GetHall", new { hallId = hallToReturn.Id }, linkedHall));
                }

                return(CreatedAtRoute("GetHall", new { hallId = hallToReturn.Id }, hallToReturn));
            }

            Mapper.Map(hallToUpdate, hallFromDb);

            _hallRepository.UpdateHall(hallFromDb);

            if (!await _hallRepository.SaveChangesAsync())
            {
                _logger.LogError($"Updating hall: {hallId} failed on save");
            }

            return(NoContent());
        }
Beispiel #2
0
        public async Task <IActionResult> PartiuallyUpdateHallByIdAsync(
            [FromRoute, SwaggerParameter(Description = "Id of hall to update", Required = true)] Guid hallId,
            [FromBody, SwaggerParameter(Description = "Jsonpatch operation document to update", Required = true)] JsonPatchDocument <HallToUpdateDto> patchDoc,
            [FromHeader(Name = "Accept"), SwaggerParameter(Description = "media type to request betwen json or json+hateoas")] string mediaType)
        {
            if (patchDoc == null)
            {
                return(BadRequest());
            }

            var hallFromDb = await _hallRepository.GetHallAsync(hallId);

            //  upserting if movie does not already exist
            if (hallFromDb == null)
            {
                var hallToCreate = new HallToUpdateDto();
                patchDoc.ApplyTo(hallToCreate, ModelState);

                if (!ModelState.IsValid)
                {
                    new ProccessingEntityObjectResultErrors(ModelState);
                }

                var hallToAddToDb = Mapper.Map <Hall>(hallToCreate);
                hallToAddToDb.Id = hallId;
                _hallRepository.AddHall(hallToAddToDb);

                if (!await _hallRepository.SaveChangesAsync())
                {
                    _logger.LogError($"Upserting hall: {hallId} failed on save");
                }

                var hallToReturn = Mapper.Map <HallDto>(hallToAddToDb);

                if (mediaType == "application/vnd.biob.json+hateoas")
                {
                    var links = CreateLinksForHall(hallToReturn.Id);

                    var linkedHall = hallToReturn.ShapeData(null) as IDictionary <string, object>;

                    linkedHall.Add("links", links);

                    return(CreatedAtRoute("GetHall", new { hallId = hallToReturn.Id }, linkedHall));
                }

                return(CreatedAtRoute("GetHall", new { hallId = hallToReturn.Id }, hallToReturn));
            }

            var hallToPatch = Mapper.Map <HallToUpdateDto>(hallFromDb);

            patchDoc.ApplyTo(hallToPatch, ModelState);

            if (!ModelState.IsValid)
            {
                new ProccessingEntityObjectResultErrors(ModelState);
            }

            Mapper.Map(hallToPatch, hallFromDb);
            _hallRepository.UpdateHall(hallFromDb);

            if (!await _hallRepository.SaveChangesAsync())
            {
                _logger.LogError($"Partially updating hall: {hallId} failed on save");
            }

            return(NoContent());
        }