예제 #1
0
        private void SaveReport(SqlConnection connection, HkpvReport report, HkpvReportInfo info, int institutionId)
        {
            using (var ms = report.WriteToStream(asJson: false, compressed: true))
            {
                var userId = this.GetRowId("Users", _authContext.Principal.Identity.Name, connection);

                SqlCommand insert = new SqlCommand("insert into Messages([UserId], [InstitutionId], [Hash_SHA256], [Month], [Year], [Date], [Data]) values(@userId, @institutionId, @hash, @month, @year, @date, @data)", connection);
                insert.Parameters.AddWithValue("@userId", userId);
                insert.Parameters.AddWithValue("@institutionId", institutionId);
                insert.Parameters.AddWithValue("@hash", info.HashSHA256);
                insert.Parameters.AddWithValue("@month", info.Month);
                insert.Parameters.AddWithValue("@year", info.Year);
                insert.Parameters.AddWithValue("@date", DateTime.Now);
                insert.Parameters.AddWithValue("@data", ms.ToArray());
                try
                {
                    insert.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, ex.Message);
                    throw ex;
                }

                _logger.LogInformation("Report saved");
            }
        }
예제 #2
0
        private HkpvReportInfo GetLast(string institution, int institudionId, SqlConnection connection)
        {
            var command = new SqlCommand($"SELECT top 1 [Id],[Month],[Year],[Hash_SHA256],[Date] from [Messages] where [InstitutionId] =  @institudionId ORDER BY [Date] desc", connection);

            command.Parameters.AddWithValue("@institudionId", institudionId);

            using (var reader = command.ExecuteReader())
            {
                if (reader.Read())
                {
                    var info = new HkpvReportInfo()
                    {
                        Id          = reader.GetInt32(0),
                        Month       = reader.GetInt16(1),
                        Year        = reader.GetInt16(2),
                        HashSHA256  = reader.GetString(3),
                        Created     = reader.GetDateTime(4),
                        Institution = institution
                    };

                    return(info);
                }
            }


            return(null);
        }
예제 #3
0
        private void Save(HkpvReport report)
        {
            if (_authContext?.Principal == null)
            {
                _logger.LogError("Not logged in");
                throw new Exception("Not logged in");
            }

            using (var connection = new SqlConnection(_connectionString))
            {
                connection.Open();

                var institutionId = this.GetRowId("Institutions", report.Institution.Id, connection);
                var lastInfo      = GetLast(report.Institution.Id, institutionId, connection);

                var info = HkpvReportInfo.Create(report, lastInfo?.Id ?? -1, lastInfo?.Created ?? DateTime.Now);

                if (lastInfo != null && info.Equals(lastInfo))
                {
                    _logger.LogInformation("Report already exits. Skip saving.");
                    return;
                }

                this.SaveReport(connection, report, info, institutionId);
            }
        }
예제 #4
0
        private (HkpvReportInfo info, string filename) GetLastFile(string institution)
        {
            var currentId = Directory.GetFiles(_path).OrderByDescending(x => x)
                            .Select(x => Path.GetFileName(x))
                            .Select(x => new { Filename = x, Match = _filenamePattern.Match(x) })
                            .Where(x => x.Match.Success)
                            .Where(x => string.IsNullOrEmpty(institution) || string.Equals(x.Match.Groups["institution"].Value, institution, StringComparison.CurrentCultureIgnoreCase))
                            .FirstOrDefault();

            if (currentId != null)
            {
                var info = new HkpvReportInfo()
                {
                    Id          = int.Parse(currentId.Match.Groups["id"].Value),
                    Institution = currentId.Match.Groups["institution"].Value,
                    Year        = int.Parse(currentId.Match.Groups["year"].Value),
                    Month       = int.Parse(currentId.Match.Groups["month"].Value),
                    HashSHA256  = currentId.Match.Groups["hash"].Value
                };

                return(info, currentId.Filename);
            }

            return(null, string.Empty);
        }