< Summary

Information
Class: Services.LabelServices.OzonLabelSessionStore
Assembly: Services
File(s): /home/runner/work/Studio2Prod/Studio2Prod/AppRepository/LabelServices/OzonLabelSessionStore.cs
Line coverage
100%
Covered lines: 30
Uncovered lines: 0
Coverable lines: 30
Total lines: 72
Line coverage: 100%
Branch coverage
93%
Covered branches: 28
Total branches: 30
Branch coverage: 93.3%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SaveLabels(...)90.9%2222100%
ClearSession(...)100%88100%
CreateSessionKey()100%11100%

File(s)

/home/runner/work/Studio2Prod/Studio2Prod/AppRepository/LabelServices/OzonLabelSessionStore.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Http;
 2using Newtonsoft.Json;
 3
 4namespace Services.LabelServices;
 5
 6public class OzonLabelSessionStore
 7{
 8    public string SaveLabels(ISession session, string? existingSessionKey, Dictionary<string, byte[]> labels, bool appen
 9    {
 1010        if (labels == null || labels.Count == 0)
 11        {
 212            return existingSessionKey ?? CreateSessionKey();
 13        }
 14
 815        var sessionKey = !string.IsNullOrEmpty(existingSessionKey) && append
 816            ? existingSessionKey
 817            : (existingSessionKey ?? CreateSessionKey());
 18
 19        List<string> keys;
 820        if (append && !string.IsNullOrEmpty(existingSessionKey))
 21        {
 122            var existingJson = session.GetString(sessionKey);
 123            keys = string.IsNullOrEmpty(existingJson)
 124                ? new List<string>()
 125                : JsonConvert.DeserializeObject<List<string>>(existingJson) ?? new List<string>();
 26        }
 27        else
 28        {
 729            keys = new List<string>();
 30        }
 31
 3832        foreach (var label in labels)
 33        {
 1134            if (!keys.Contains(label.Key))
 35            {
 1036                keys.Add(label.Key);
 37            }
 38
 1139            var fileKey = $"{sessionKey}_{label.Key}";
 1140            session.Set(fileKey, label.Value);
 41        }
 42
 843        session.SetString(sessionKey, JsonConvert.SerializeObject(keys));
 844        return sessionKey;
 45    }
 46
 47    public void ClearSession(ISession session, string? sessionKey)
 48    {
 349        if (string.IsNullOrEmpty(sessionKey))
 50        {
 251            return;
 52        }
 53
 154        var labelKeysJson = session.GetString(sessionKey);
 155        if (!string.IsNullOrEmpty(labelKeysJson))
 56        {
 157            var labelKeys = JsonConvert.DeserializeObject<List<string>>(labelKeysJson);
 158            if (labelKeys != null)
 59            {
 660                foreach (var key in labelKeys)
 61                {
 262                    var fileKey = $"{sessionKey}_{key}";
 263                    session.Remove(fileKey);
 64                }
 65            }
 66        }
 67
 168        session.Remove(sessionKey);
 169    }
 70
 271    public static string CreateSessionKey() => $"labels_{Guid.NewGuid()}";
 72}