예제 #1
0
        public IActionResult UpdateForumMessage([FromHeader] string key, [FromBody] ForumMessageDTO updatedForumMess)
        {
            if (!_authorizationService.AuthorizeUser(key))
            {
                return(StatusCode(StatusCodes.Status401Unauthorized, "User authorization failed!"));
            }

            var newForumMess = mapper.Map <ForumMessage>(updatedForumMess);

            try
            {
                _forumMessageService.UpdateForumMessage(updatedForumMess);
                var res = mapper.Map <ForumMessage>(newForumMess);

                return(Ok(res));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "Error updating forum message: " + ex.Message);

                return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message));
            }
        }
        public void UpdateForumMessage(ForumMessageDTO updatedForumMessage)
        {
            if (_forumMessageRepository.GetForumMessageByID(updatedForumMessage.ForumMessageID) == null)
            {
                throw new NotFoundException("Forum message with that ID does not exist!");
            }

            var oldForumMess = _forumMessageRepository.GetForumMessageByID(updatedForumMessage.ForumMessageID);
            var newForumMess = mapper.Map <ForumMessage>(updatedForumMessage);

            if (oldForumMess.ForumMessageID != newForumMess.ForumMessageID)
            {
                throw new ErrorOccurException("Forum message ID can not be changed!");
            }

            if (newForumMess.SenderID != oldForumMess.SenderID)
            {
                throw new ErrorOccurException("Forum message sender ID can not be changed!");
            }

            if (newForumMess.Title != oldForumMess.Title)
            {
                throw new ErrorOccurException("Forum message title can not be changed!");
            }

            try
            {
                newForumMess.SenderID = oldForumMess.SenderID;

                mapper.Map(newForumMess, oldForumMess);
                _forumMessageRepository.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new ErrorOccurException("Error updating forum message: " + ex.Message);
            }
        }