コード例 #1
0
        public ActionResult AddComment(CommentDto commentDto)
        {
            var list = new List <CommentViewModel>();

            try
            {
                var userId  = User.Identity.GetUserId();
                var comment = _repository.PostRepository.AddComment(commentDto.PostId, userId, commentDto.Text);
                list.Add(CommentViewModel.Create(comment, userId));
                _repository.Complete();
                list[0].Id = comment.Id;
                CommentsHub.Notify(commentDto.PostId, comment.Id);
            }
            catch (DbEntityValidationException exception)
            {
                list[0].Error = DbEntityValidationExceptionHandler.GetExceptionMessage(exception);
                return(PartialView("_CommentsList", list));
            }
            catch (Exception exception)
            {
                list[0].Error = exception.Message;
                return(PartialView("_CommentsList", list));
            }

            return(PartialView("_CommentsList", list));
        }
コード例 #2
0
        public IHttpActionResult UploadMessage(string username, MessageDto messageDto)
        {
            try
            {
                var userId = User.Identity.GetUserId();
                var output = Repository.MessengerRepository.UploadMessage(userId,
                                                                          Repository.ApplicationUserRepository.GetUserByUsername(username).Id, messageDto);
                Repository.Complete();

                Repository.MessengerRepository.Send(userId, username, output.Id + "");

                return(Ok(output.Id));
            }
            catch (AuthenticationException)
            {
                return(Unauthorized());
            }
            catch (DbEntityValidationException exception)
            {
                return(BadRequest(DbEntityValidationExceptionHandler.GetExceptionMessage(exception)));
            }
            catch (Exception exception)
            {
                return(BadRequest(exception.Message));
            }
        }
コード例 #3
0
        public override void Handle(ExceptionHandlerContext context)
        {
            IConcreteExceptionHandler exceptionHandler = null;
            var exception = context.Exception;

            if (exception is Helpers.ValidationException)
            {
                string message = new StringBuilder().Append(LocalizedText.ValidationFailedMainCommunicate)
                                 .Append(" \n")
                                 .Append(context.Exception.Message)
                                 .ToString();

                exceptionHandler = new UniversalExceptionHandler(context, message, HttpStatusCode.BadRequest);
            }
            else if (context.Exception is DbEntityValidationException)
            {
                exceptionHandler = new DbEntityValidationExceptionHandler(context);
            }
            else if (context.Exception is ValidationException)
            {
                exceptionHandler = new FluentValidationExceptionHandler(context);
            }
            else
            {
                exceptionHandler = new UniversalExceptionHandler(context);
            }

            exceptionHandler.Handle();
        }
コード例 #4
0
        private void FillExceptions()
        {
            Handlers = new List <BaseExceptionHandler>();
            Handlers.Add(ApiBusinessExceptionHandler.GetInstance());
            Handlers.Add(ApiDataExceptionHandler.GetInstance());
            Handlers.Add(ApiExceptionHandler.GetInstance());
            Handlers.Add(DbEntityValidationExceptionHandler.GetInstance());
            Handlers.Add(GenericExceptionHandler.GetInstance());
            BaseExceptionHandler _handler;

            for (int i = 0; i <= Handlers.Count(); i++)
            {
                _handler = Handlers.ElementAt(i);
                if (i + 1 == Handlers.Count())
                {
                    break;
                }
                _handler.Mychainhandler = Handlers.ElementAt(i + 1);
            }
        }
コード例 #5
0
        protected IHttpActionResult Post(Func <string, dynamic> func)
        {
            try
            {
                var userId = User.Identity.GetUserId();
                var output = func(userId);
                Repository.Complete();

                return(Ok(output.Id));
            }
            catch (AuthenticationException)
            {
                return(Unauthorized());
            }
            catch (DbEntityValidationException exception)
            {
                return(BadRequest(DbEntityValidationExceptionHandler.GetExceptionMessage(exception)));
            }
            catch (Exception exception)
            {
                return(BadRequest(exception.Message));
            }
        }
コード例 #6
0
        protected IHttpActionResult Put(Action <string> action)
        {
            try
            {
                var userId = User.Identity.GetUserId();
                action(userId);
                Repository.Complete();
            }
            catch (AuthenticationException)
            {
                return(Unauthorized());
            }
            catch (DbEntityValidationException exception)
            {
                return(BadRequest(DbEntityValidationExceptionHandler.GetExceptionMessage(exception)));
            }
            catch (Exception exception)
            {
                return(BadRequest(exception.Message));
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }