Пример #1
0
        public void Validate(ValidateArgs args)
        {
            var report = ReadReport(args.File);

            var result = report.Validate();

            var formatter = new HkpvReportValidationResultFormatter(ResultFormatterTemplate.Text);
            var message   = formatter.Format(report, result);

            if (!result.IsValid)
            {
                HandleFailure(message);
            }
            else if (!string.IsNullOrEmpty(message))
            {
                Console.WriteLine(message);
            }
            else
            {
                Console.WriteLine("ok");
            }
        }
Пример #2
0
        public async Task HandlePut(HttpContext context)
        {
            if (context.Request.Method != HttpMethods.Put && context.Request.Method != HttpMethods.Post)
            {
                await HandleDefault(context);

                return;
            }

            await EnsureIsAuthenticated(context);

            if (!IsAuthenticated(context))
            {
                context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                return;
            }

            int.TryParse((string)context.GetRouteValue("year"), out int year);
            int.TryParse((string)context.GetRouteValue("month"), out int month);


            if (year < 2000 || year > DateTime.Today.Year)
            {
                await RespondError(context, $"Ungültiges Jahr '{context.GetRouteValue("year")}'");

                return;
            }

            if (month < 1 || month > 12)
            {
                await RespondError(context, $"Ungültiger Monat '{context.GetRouteValue("month")}'");

                return;
            }

            HkpvReport report;

            try
            {
                report = new HkpvReportSerializer().Deserialize(context.Request.Body);
            }
            catch (Exception e)
            {
                _logger?.LogError(e, "Deserialize failed.");
                report = null;
            }

            if (report == null)
            {
                await RespondError(context, $"Die Daten können nicht gelesen werden.");

                return;
            }

            var date = new DateTime(year, month, 1);

            if (report.FromD != date)
            {
                await RespondError(context, $"Ungültiger Zeitraum: {year}-{month}, entspricht nicht {report.From}.");

                return;
            }

            var validationResult = await new HkpvReportValidator().ValidateAsync(report);

            var msg = new HkpvReportValidationResultFormatter(ResultFormatterTemplate.Text).Format(report, validationResult);

            if (!validationResult.IsValid)
            {
                await RespondError(context, msg);

                return;
            }

            var saveCmd = new HkpvReportSaveCommand()
            {
                Report = report
            };

            var engine = _engineFactory();

            engine.Login(context.User);
            engine.Execute(saveCmd);

            await RespondSuccess(context, msg);
        }