コード例 #1
0
        public async Task <ActionResult <object> > PostFavorite(DTO.FavoriteDTO dto)
        {
            var favorite = new Favorite
            {
                Link   = dto.Link,
                Title  = dto.Title,
                IdUser = int.Parse(User.Identity.Name)
            };

            _context.Favorites.Add(favorite);
            await _context.SaveChangesAsync();

            return(new { Id = favorite.IdFavorite, Link = favorite.Link, Title = favorite.Title });
        }
コード例 #2
0
        public async Task <IActionResult> PutFavorite(int id, DTO.FavoriteDTO dto)
        {
            var favorite = await _context.Favorites.FindAsync(id);

            if (favorite == null)
            {
                return(NotFound());
            }

            if (id != favorite.IdFavorite)
            {
                return(BadRequest());
            }

            favorite.Link  = dto.Link;
            favorite.Title = dto.Title;

            _context.Entry(favorite).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FavoriteExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }