Esempio n. 1
0
        /// <summary>
        /// Method that maps news from dto and sends News object to repo. Then it maps it for return.
        /// </summary>
        /// <param name="newsCreateDto">NewsCreateDto object</param>
        /// <returns>NewsReadDto object</returns>
        public async Task <NewsReadDto> AddNewsAsync(NewsCreateEditDto newsCreateDto)
        {
            var news = _mapper.Map <Core.Models.Domain.News>(newsCreateDto);

            news = await _newsRepository.AddNewsAsync(news);

            var result = _mapper.Map <NewsReadDto>(news);

            return(result);
        }
Esempio n. 2
0
        public async Task <IActionResult> AddNewsAsync([FromBody] NewsCreateEditDto newsCreateDto)
        {
            try
            {
                var result = await _newsService.AddNewsAsync(newsCreateDto);

                if (result == null)
                {
                    return(BadRequest("ERROR adding news"));
                }

                return(CreatedAtRoute(nameof(GetNewsByIdAsync), new { id = result.NewsId }, result));
            }
            catch (Exception e)
            {
                return(BadRequest("Something went wrong: " + e.Message));
            }
        }
Esempio n. 3
0
        public async Task <IActionResult> EditNewsAsync([FromRoute] int newsId, [FromBody] NewsCreateEditDto newsEditDto)
        {
            try
            {
                var result = await _newsService.EditNewsAsync(newsId, newsEditDto);

                if (!result)
                {
                    return(BadRequest("ERROR editing news"));
                }

                return(NoContent());
            }
            catch (Exception e)
            {
                return(BadRequest("Something went wrong: " + e.Message));
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Method that maps dto to News object and calls repo.
        /// </summary>
        /// <param name="newsId">news id</param>
        /// <param name="userId">user id</param>
        /// <param name="newsEditDto">NewsCreateEditDto object</param>
        /// <returns>true if edit is successful, false otherwise</returns>
        public async Task <bool> EditNewsAsync(int newsId, NewsCreateEditDto newsEditDto)
        {
            var news = _mapper.Map <Core.Models.Domain.News>(newsEditDto);

            return(await _newsRepository.EditNewsAsync(newsId, news) && await _userEditedNewsRepository.AddUserEditAsync(newsEditDto.UserId, newsId) != null);
        }