Exemple #1
0
        public ActionResult Create(TopicCreateModel model)
        {
            bool ok = true;

            if (ModelState.IsValid)
            {
                TopicCreateDTO topicDTO = Mapper.Map <TopicCreateModel, TopicCreateDTO>(model);

                ServiceMessage serviceMessage = service.Create(topicDTO);
                if (!serviceMessage.Succeeded)
                {
                    foreach (string error in serviceMessage.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                    ok = false;
                }
            }
            else
            {
                ok = false;
            }

            return(ok ? RedirectToAction("List") as ActionResult : View(model));
        }
        public ServiceMessage Create(TopicCreateDTO topicDTO)
        {
            List <string> errors    = new List <string>();
            bool          succeeded = Validate(topicDTO, errors);

            if (succeeded)
            {
                try
                {
                    UserEntity  userEntity  = unitOfWork.Users.Get(topicDTO.CreatorLogin);
                    TopicEntity topicEntity = new TopicEntity
                    {
                        Creator           = userEntity,
                        DateCreated       = DateTime.Now,
                        DateOfLastMessage = DateTime.Now,
                        Title             = topicDTO.Title,
                        Description       = topicDTO.Description
                    };

                    unitOfWork.Topics.Add(topicEntity);
                    unitOfWork.Commit();
                }
                catch (Exception ex)
                {
                    succeeded = false;
                    ExceptionMessageBuilder.FillErrors(ex, errors);
                }
            }

            return(new ServiceMessage
            {
                Errors = errors,
                Succeeded = succeeded
            });
        }
Exemple #3
0
        public async Task <IActionResult> Create([FromBody] TopicCreateDTO topicCreateDTO)
        {
            var location = GetControllerActionNames();

            try
            {
                _logger.LogInfo(LogMessages.AttemptedToCreate(location));
                if (topicCreateDTO == null)
                {
                    _logger.LogWarn(LogMessages.EmptyRequest(location));
                    return(BadRequest(ModelState));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogWarn(LogMessages.IncompleteData(location));
                    return(BadRequest(ModelState));
                }
                var topic     = _mapper.Map <Topic>(topicCreateDTO);
                var isSuccess = await _topicRepository.Create(topic);

                if (!isSuccess)
                {
                    return(InternalError(LogMessages.CreateFailed(location)));
                }
                _logger.LogInfo(LogMessages.Success(location));
                _logger.LogInfo($"{topic}");
                return(Created("Create", new { topic }));
            }
            catch (Exception e)
            {
                return(InternalError(LogMessages.InternalError(location, e.Message, e.InnerException)));
            }
        }
        private bool Validate(TopicCreateDTO topic, ICollection <string> errors)
        {
            bool validated = true;

            if (String.IsNullOrEmpty(topic.CreatorLogin))
            {
                validated = false;
                errors.Add("Login of sender is required");
            }
            else if (!unitOfWork.Users.Exists(topic.CreatorLogin))
            {
                validated = false;
                errors.Add("User with such login was not found");
            }

            if (String.IsNullOrEmpty(topic.Title))
            {
                validated = false;
                errors.Add("Title cannot be empty");
            }
            else if (topic.Title.Length > 50)
            {
                validated = false;
                errors.Add("Title must be less than 50 characters");
            }

            if (String.IsNullOrEmpty(topic.Description))
            {
                validated = false;
                errors.Add("Description cannot be empty");
            }
            else if (topic.Description.Length > 200)
            {
                validated = false;
                errors.Add("Description must be less than 200 characters");
            }

            return(validated);
        }