using System.Xml;

namespace Kit.Helpers.Repository.Xml;
public interface IRowVersionRepository
{
    int GetRowVersion(XmlNode xmlNode);
    void SetRowVersion(XmlNode xmlNode, int newVersionId);
    int IncreaseRowVersion(XmlNode xmlNode);
}

public class RowVersionRepository : IRowVersionRepository
{
    public RowVersionRepository() { }

    private readonly string _attrNameRowVersion = "RowVersion";

    public int GetRowVersion(XmlNode xmlNode) => xmlNode.GetValueAttributeInt32(_attrNameRowVersion);

    public void SetRowVersion(XmlNode xmlNode, int newVersionId) => xmlNode.SetAttributeValue(_attrNameRowVersion, newVersionId.ToString());

    public int IncreaseRowVersion(XmlNode xmlNode)
    {
        int newVersionId = GetRowVersion(xmlNode) + 1;

        SetRowVersion(xmlNode, newVersionId);

        return newVersionId;
    }
}