Beispiel #1
0
        public ServiceResult <TopicServiceResultCode> AddTopic(AddOrUpdateTopicRequest request)
        {
            var isValidRequest = ValidateAddTopicRequest(request);

            if (!isValidRequest)
            {
                return(ArgumentErrorResult(TopicServiceResultCode.ArgumentError));
            }

            var topic = new Topic {
                Id = Guid.NewGuid(), CourseId = request.CourseId, Title = request.Title, DisplayOrder = request.DisplayOrder
            };

            try
            {
                _topicRepo.Add(topic);
                _topicRepo.SaveChanges();
                return(BuildResult(true, TopicServiceResultCode.Success, resultObj: topic));
            }
            catch (Exception ex)
            {
                _systemErrorsRepo.AddLog(ex);
                return(InternalErrorResult(TopicServiceResultCode.BackendException));
            }
        }
Beispiel #2
0
        private bool ValidateUpdateTopicRequest(AddOrUpdateTopicRequest request)
        {
            var isValidData = ValidateAddTopicRequest(request);
            var isValidKey  = !(request.Id == Guid.Empty);

            return(isValidData && isValidKey);
        }
Beispiel #3
0
        public ServiceResult <TopicServiceResultCode> UpdateTopic(AddOrUpdateTopicRequest request)
        {
            var isValidRequest = ValidateUpdateTopicRequest(request);

            if (!isValidRequest)
            {
                return(ArgumentErrorResult(TopicServiceResultCode.ArgumentError));
            }

            try
            {
                var targetTopic = _topicRepo.Get(request.Id);
                if (targetTopic == null)
                {
                    return(DataErrorResult(TopicServiceResultCode.DataNotExist));
                }
                UpdateDataOfTopic(targetTopic, request);
                _topicRepo.Update(targetTopic);
                _topicRepo.SaveChanges();
                return(BuildResult(true, TopicServiceResultCode.Success, resultObj: targetTopic));
            }
            catch (Exception ex)
            {
                _systemErrorsRepo.AddLog(ex);
                return(InternalErrorResult(TopicServiceResultCode.BackendException));
            }
        }
Beispiel #4
0
        private bool ValidateAddTopicRequest(AddOrUpdateTopicRequest request)
        {
            if (request == null)
            {
                return(false);
            }
            if (request.CourseId == Guid.Empty)
            {
                return(false);
            }
            if (string.IsNullOrWhiteSpace(request.Title))
            {
                return(false);
            }
            if (request.DisplayOrder <= 0)
            {
                return(false);
            }

            return(true);
        }
Beispiel #5
0
 private void UpdateDataOfTopic(Topic topic, AddOrUpdateTopicRequest request)
 {
     topic.Title        = request.Title;
     topic.DisplayOrder = request.DisplayOrder;
 }