Skip to content
Snippets Groups Projects
Commit 8c368499 authored by Michal Strnad's avatar Michal Strnad
Browse files

Issue #34 - Implement LockAuction operation

parent 66377e9f
No related branches found
No related tags found
No related merge requests found
......@@ -198,6 +198,7 @@ src/libfred/registrable_object/domain/auction/create_auction.cc
src/libfred/registrable_object/domain/auction/delete_auction.cc
src/libfred/registrable_object/domain/auction/finish_auction.cc
src/libfred/registrable_object/domain/auction/get_auctions.cc
src/libfred/registrable_object/domain/auction/lock_auction.cc
src/libfred/registrable_object/domain/auction/release_fqdn.cc
src/libfred/registrable_object/domain/auction/update_auction.cc
src/libfred/registrable_object/domain/check_domain.cc
......
/*
* Copyright (C) 2024 CZ.NIC, z. s. p. o.
*
* This file is part of FRED.
*
* FRED is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FRED is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FRED. If not, see <https://www.gnu.org/licenses/>.
*/
#include "src/libfred/registrable_object/domain/auction/lock_auction.hh"
#include "liblog/liblog.hh"
namespace LibFred {
namespace Domain {
namespace Auction {
LockAuction::LockAuction(const OperationContext& ctx, const AuctionId& auction)
: ctx_{ctx}
{
const auto dbres = this->get_ctx().get_conn().exec_params(
"SELECT "
"FROM domain_auction "
"WHERE id = $1::BIGINT "
"FOR UPDATE",
Database::QueryParams{*auction});
if (dbres.size() <= 0)
{
struct NotFound : AuctionDoesNotExist
{
const char* what() const noexcept override { return "auction does not exist"; }
};
throw NotFound{};
}
}
LockAuction::LockAuction(const OperationContext& ctx, const GetAuctions::AuctionInfo& auction)
: ctx_{ctx}
{
const auto dbres = this->get_ctx().get_conn().exec_params(
"SELECT fqdn = $2::TEXT, "
"external_id IS NULL, "
"next_event_after < NOW() IS NOT TRUE AND winner_id IS NULL AND external_id IS NOT NULL, "
"next_event_after < NOW() AND winner_id IS NULL AND external_id IS NOT NULL, "
"NOW() < win_expires_at AND external_id IS NOT NULL, "
"win_expires_at IS NULL AND winner_id IS NOT NULL AND external_id IS NOT NULL, "
"win_expires_at <= NOW() AND external_id IS NOT NULL "
"FROM domain_auction "
"WHERE id = $1::BIGINT "
"FOR UPDATE",
Database::QueryParams{*auction.auction_id, auction.fqdn});
if (dbres.size() <= 0)
{
struct NotFound : AuctionDoesNotExist
{
const char* what() const noexcept override { return "auction does not exist"; }
};
throw NotFound{};
}
if (1 < dbres.size())
{
struct TooManyRows : std::exception
{
const char* what() const noexcept override { return "more then one row selected"; }
};
throw TooManyRows{};
}
const auto column = dbres[0];
if (!static_cast<bool>(column[0]))
{
struct DifferentFqdn : AuctionChanged
{
const char* what() const noexcept override { return "fqdn differs"; }
};
throw DifferentFqdn{};
}
const auto property = [&column]()
{
if (static_cast<bool>(column[1]))
{
return GetAuctions::Property::without_external_auction_id;
}
if (static_cast<bool>(column[2]))
{
return GetAuctions::Property::waiting_for_event;
}
if (static_cast<bool>(column[3]))
{
return GetAuctions::Property::event_anticipated;
}
if (static_cast<bool>(column[4]))
{
return GetAuctions::Property::waiting_for_registration;
}
if (static_cast<bool>(column[5]))
{
return GetAuctions::Property::registration_completed;
}
if (static_cast<bool>(column[6]))
{
return GetAuctions::Property::registration_expired;
}
struct NoProperty : AuctionChanged
{
const char* what() const noexcept override { return "no property"; }
};
throw NoProperty{};
}();
if (property != auction.property)
{
struct DifferentProperty : AuctionChanged
{
const char* what() const noexcept override { return "property differs"; }
};
throw DifferentProperty{};
}
}
const OperationContext& LockAuction::get_ctx() const noexcept
{
return ctx_;
}
}//namespace LibFred::Domain::Auction
}//namespace LibFred::Domain
}//namespace LibFred
/*
* Copyright (C) 2024 CZ.NIC, z. s. p. o.
*
* This file is part of FRED.
*
* FRED is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FRED is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FRED. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef LOCK_AUCTION_HH_00601192CF777FA43E54F7A2F1407CD4//date "+%s.%N"|md5sum|tr "[a-f]" "[A-F]"
#define LOCK_AUCTION_HH_00601192CF777FA43E54F7A2F1407CD4
#include "libfred/registrable_object/domain/auction/get_auctions.hh"
#include "libfred/opcontext.hh"
#include <exception>
namespace LibFred {
namespace Domain {
namespace Auction {
/**
* Lock given auction to protect against concurrent updates.
*/
class LockAuction
{
public:
struct Exception : std::exception {};
struct AuctionDoesNotExist : Exception {};
struct AuctionChanged : Exception {};
/**
* Lock given row for update
* @param ctx contains reference to database transaction
*/
explicit LockAuction(const OperationContext& ctx, const AuctionId& auction);
/**
* Lock given row for update and check for equality
* @param ctx contains reference to database transaction
*/
explicit LockAuction(const OperationContext& ctx, const GetAuctions::AuctionInfo& auction);
const OperationContext& get_ctx() const noexcept;
private:
const OperationContext& ctx_;
};
}//namespace LibFred::Domain::Auction
}//namespace LibFred::Domain
}//namespace LibFred
#endif//LOCK_AUCTION_HH_00601192CF777FA43E54F7A2F1407CD4
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment