public async Task <ActionResult <HobbyDto> > PutHobby(int hobbyId, HobbyDto hobbyDto)
        {
            try
            {
                var oldHobby = await _hobbyRepository.GetHobby(hobbyId);

                if (oldHobby == null)
                {
                    return(NotFound($"There's no hobby with that id: {hobbyId}"));
                }

                var newHobby = _mapper.Map(hobbyDto, oldHobby);
                _hobbyRepository.Update(newHobby);

                if (await _hobbyRepository.Save())
                {
                    return(NoContent());
                }
            }
            catch (Exception e)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, $"Database Failure: {e.Message}"));
            }
            return(BadRequest());
        }
        private IEnumerable <LinkDto> CreateLinksForHobbies(HobbyDto hobbyDto)
        {
            var links = new[]
            {
                new LinkDto
                {
                    Href   = Url.Link("GetHobby", new { hobbyId = hobbyDto.HobbyId }).ToLower(),
                    Rel    = "self",
                    Method = "GET"
                },
                new LinkDto
                {
                    Href   = Url.Link("PutHobby", new { hobbyId = hobbyDto.HobbyId }).ToLower(),
                    Rel    = "update hobby",
                    Method = "PUT"
                },
                new LinkDto
                {
                    Href   = Url.Link("DeleteHobby", new { hobbyId = hobbyDto.HobbyId }).ToLower(),
                    Rel    = "delete hobby",
                    Method = "DELETE"
                }
            };

            return(links);
        }
Exemple #3
0
        public async Task Update(HobbyDto hobby)
        {
            if (hobby.Uuid == Guid.Empty || string.IsNullOrEmpty(hobby.Name))
            {
                throw new UnprocessableException();
            }

            await _hobbyDal.Update(hobby);
        }
Exemple #4
0
        public async Task Add(HobbyDto hobby)
        {
            if (string.IsNullOrEmpty(hobby.Name))
            {
                throw new UnprocessableException();
            }

            await _hobbyDal.Add(hobby);
        }
        [HttpPost("add")]//ROUTE
        public IActionResult Add([FromBody] HobbyDto hobbyDto)
        {
            // map dto to entity
            var hobby = _mapper.Map <Hobby>(hobbyDto);

            try
            {
                // save
                _hobbyService.Add(hobby);
                return(Ok(hobby));
            }
            catch (AppException ex)
            {
                // return error message if there was an exception
                return(BadRequest(new { message = ex.Message }));
            }
        }
        public async Task <ActionResult <HobbyDto> > PostHobby(HobbyDto hobbyDto)
        {
            try
            {
                var mappedEntity = _mapper.Map <HobbyDto>(hobbyDto);
                _hobbyRepository.Add(mappedEntity);

                if (await _hobbyRepository.Save())
                {
                    return(Created($"api/v1.0/cities/{mappedEntity.HobbyId}", _mapper.Map <HobbyDto>(mappedEntity)));
                }
            }
            catch (Exception e)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, $"Database Failure: {e.Message}"));
            }
            return(BadRequest());
        }
        [HttpPut("{id}")]//ROUTE
        public IActionResult Update(int id, [FromBody] HobbyDto hobbyDto)
        {
            // map dto to entity and set id
            var hobby = _mapper.Map <Hobby>(hobbyDto);

            hobby.IdHobby = id;

            try
            {
                // save
                _hobbyService.Update(hobby);
                return(Ok());
            }
            catch (AppException ex)
            {
                // return error message if there was an exception
                return(BadRequest(new { message = ex.Message }));
            }
        }
Exemple #8
0
 public async Task Update(HobbyDto hobby)
 {
     _context.Hobby.Update(hobby);
     await _context.SaveChangesAsync();
 }
Exemple #9
0
        public async Task Add(HobbyDto hobby)
        {
            await _context.Hobby.AddAsync(hobby);

            await _context.SaveChangesAsync();
        }