public async Task <IActionResult> Post([FromBody] OutputShippingViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                var excpetion = new
                {
                    error = ResultFormatter.FormatErrorMessage(ModelState)
                };
                return(new BadRequestObjectResult(excpetion));
            }
            try
            {
                VerifyUser();
                ValidateService.Validate(viewModel);
                var result = await _service.Create(viewModel);

                return(Created("/", result));
            }
            catch (ServiceValidationException ex)
            {
                var Result = new
                {
                    error      = ResultFormatter.Fail(ex),
                    apiVersion = "1.0.0",
                    statusCode = HttpStatusCode.BadRequest,
                    message    = "Data does not pass validation"
                };

                return(new BadRequestObjectResult(Result));
            }
            catch (Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message));
            }
        }
Beispiel #2
0
        public async Task Should_NotValid_Post()
        {
            var dataUtil = new OutputShippingViewModel();
            //v
            var serviceMock = new Mock <IOutputShippingService>();

            serviceMock.Setup(s => s.Create(It.IsAny <OutputShippingViewModel>())).ReturnsAsync(1);
            var service = serviceMock.Object;

            var identityProviderMock = new Mock <IIdentityProvider>();
            var identityProvider     = identityProviderMock.Object;

            var validateServiceMock = new Mock <IValidateService>();

            validateServiceMock.Setup(s => s.Validate(It.IsAny <OutputShippingViewModel>()))
            .Verifiable();
            var validateService = validateServiceMock.Object;

            var controller = GetController(service, identityProvider, validateService);

            controller.ModelState.AddModelError("test", "test");
            //controller.ModelState.IsValid == false;
            var response = await controller.Post(dataUtil);

            Assert.Equal((int)HttpStatusCode.BadRequest, GetStatusCode(response));
        }
        public async Task <IActionResult> GetPdfById([FromRoute] int id, [FromRoute] bool type, [FromHeader(Name = "x-timezone-offset")] string timezone)
        {
            try
            {
                var model = new OutputShippingViewModel();

                if (!type)
                {
                    model = await _service.ReadById(id);
                }
                else
                {
                    model = await _service.ReadByIdBon(id);
                }

                if (model == null)
                {
                    var Result = new
                    {
                        apiVersion = "1.0.0",
                        statusCode = HttpStatusCode.NotFound,
                        message    = "Not Found"
                    };
                    return(NotFound(Result));
                }
                else
                {
                    int timeoffsset = Convert.ToInt32(timezone);
                    var pdfTemplate = new OutputShippingPdfTemplate(model, timeoffsset);
                    var stream      = pdfTemplate.GeneratePdfTemplate();
                    return(new FileStreamResult(stream, "application/pdf")
                    {
                        FileDownloadName = string.Format("{0}.pdf", model.BonNo)
                    });
                }
            }
            catch (Exception e)
            {
                var Result = new
                {
                    apiVersion = "1.0.0",
                    statusCode = HttpStatusCode.InternalServerError,
                    message    = e.Message
                };
                return(StatusCode((int)HttpStatusCode.InternalServerError, Result));
            }
        }
        public async Task <IActionResult> Put([FromRoute] int id, [FromBody] OutputShippingViewModel viewModel)
        {
            VerifyUser();
            if (!ModelState.IsValid)
            {
                var exception = new
                {
                    error = ResultFormatter.FormatErrorMessage(ModelState)
                };
                return(new BadRequestObjectResult(exception));
            }

            try
            {
                VerifyUser();
                ValidateService.Validate(viewModel);
                await _service.Update(id, viewModel);

                return(NoContent());
            }
            catch (ServiceValidationException ex)
            {
                var Result = new
                {
                    error      = ResultFormatter.Fail(ex),
                    apiVersion = "1.0.0",
                    statusCode = HttpStatusCode.BadRequest,
                    message    = "Data does not pass validation"
                };

                return(new BadRequestObjectResult(Result));
            }
            catch (Exception ex)
            {
                var error = new
                {
                    statusCode = HttpStatusCode.InternalServerError,
                    error      = ex.Message
                };
                return(StatusCode((int)HttpStatusCode.InternalServerError, error));
            }
        }
Beispiel #5
0
        public void Should_Validator_Success()
        {
            var dataUtil = new OutputShippingViewModel()
            {
                Date = DateTimeOffset.UtcNow.AddHours(-5)
            };
            var validator = new OutputShippingValidator();
            var result    = validator.Validate(dataUtil);

            Assert.NotEqual(0, result.Errors.Count);

            dataUtil.Date = DateTimeOffset.UtcNow.AddDays(1);
            result        = validator.Validate(dataUtil);
            Assert.NotEqual(0, result.Errors.Count);

            dataUtil.Date = DateTimeOffset.UtcNow.AddDays(-1);
            result        = validator.Validate(dataUtil);
            Assert.NotEqual(0, result.Errors.Count);

            dataUtil.ShippingProductionOrders = new List <OutputShippingProductionOrderViewModel>()
            {
                new OutputShippingProductionOrderViewModel()
            };
            result = validator.Validate(dataUtil);
            Assert.NotEqual(0, result.Errors.Count);

            dataUtil.ShippingProductionOrders = new List <OutputShippingProductionOrderViewModel>()
            {
                new OutputShippingProductionOrderViewModel()
                {
                    ProductionOrder = new ProductionOrder()
                }
            };
            result = validator.Validate(dataUtil);
            Assert.NotEqual(0, result.Errors.Count);
        }