PermissionsDao: Add save()

This commit is contained in:
Albert S. 2021-10-05 21:51:31 +02:00
förälder c4072a7e95
incheckning f002969cc1
3 ändrade filer med 20 tillägg och 0 borttagningar

Visa fil

@ -7,6 +7,7 @@ class PermissionsDao
public:
PermissionsDao();
virtual std::optional<Permissions> find(std::string pagename, std::string username) = 0;
virtual void save(std::string pagename, std::string username, Permissions perms) = 0;
};
#endif // PERMISSIONSDAO_H

Visa fil

@ -41,3 +41,21 @@ std::optional<Permissions> PermissionsDaoSqlite::find(std::string pagename, std:
return Permissions{permissions};
}
void PermissionsDaoSqlite::save(std::string pagename, std::string username, Permissions perms)
{
try
{
auto query =
*db
<< "INSERT OR REPLACE INTO permissions (id, permissions, userid, page) VALUES((SELECT id FROM permissions "
"WHERE page = (SELECT id FROM page WHERE name = ?) AND userid = (SELECT id FROM user WHERE username = "
"?)), ?, (SELECT id FROM user WHERE username = ?), (SELECT id FROM page WHERE name = ?))";
query << pagename << username << perms.getPermissions() << username << pagename;
query.execute();
}
catch(const sqlite::errors::no_rows &e)
{
throwFrom(e);
}
}

Visa fil

@ -9,6 +9,7 @@ class PermissionsDaoSqlite : public PermissionsDao, protected SqliteDao
PermissionsDaoSqlite();
std::optional<Permissions> find(std::string pagename, std::string username) override;
virtual void save(std::string pagename, std::string username, Permissions perms) override;
using SqliteDao::SqliteDao;
};