The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

App::Dochazka::REST::Model::Shared - functions shared by several modules within the data model

VERSION

Version 0.264

SYNOPSIS

    use App::Dochazka::REST::Model::Shared;

    ...

EXPORTS

This module provides the following exports:

  • cud (Create, Update, Delete -- for single-record statements only)

  • decode_schedule_json function (given JSON string, return corresponding hashref)

  • load (Load/Fetch/Retrieve -- single-record only)

  • noof (get total number of records in a data model table)

  • priv_by_eid

  • schedule_by_eid

FUNCTIONS

make_test_exists

Returns coderef for a function, 'test_exists', that performs a simple true/false check for existence of a record matching a scalar search key. The record must be an exact match (no wildcards).

Takes one argument: a type string $t which is concatenated with the string 'load_by_' to arrive at the name of the function to be called to execute the search.

The returned function takes a single argument: the search key (a scalar value). If a record matching the search key is found, the corresponding object (i.e. a true value) is returned. If such a record does not exist, 'undef' (a false value) is returned. If there is a DBI error, the error text is logged and undef is returned.

cud

** USE FOR SINGLE-RECORD SQL STATEMENTS ONLY ** Attempts to Create, Update, or Delete a single database record. Takes a blessed reference (activity object or employee object), a SQL statement, and a list of attributes. Overwrites attributes in the object with the RETURNING list values received from the database. Returns a status object. Call example:

    $status = cud( object => $self, sql => $sql, attrs => [ @attr ] );

decode_schedule_json

Given JSON string representation of the schedule, return corresponding HASHREF.

load

Load a database record into a hashref based on a search key. Must be specifically enabled for the class/table in question. The search key must be an exact match: this function returns only 1 or 0 records. Call, e.g., like this:

    my $status = load( 
        class => __PACKAGE__, 
        sql => $site->DOCHAZKA_ 
        key => 44 
    ); 

noof

Given the name of a data model table, returns the total number of records in the table.

    activities employees intervals locks privhistory schedhistory
    schedintvls schedules

On failure, returns undef.

priv_by_eid

Given an EID, and, optionally, a timestamp, returns the employee's priv level as of that timestamp, or as of "now" if no timestamp was given. The priv level will default to 'passerby' if it can't be determined from the database.

schedule_by_eid

Given an EID, and, optionally, a timestamp, returns the employee's schedule as of that timestamp, or as of "now" if no timestamp was given. The schedule will default to '{}' if it can't be determined from the database.

_st_by_eid

Function that 'priv_by_eid' and 'schedule_by_eid' are wrappers of.

get_history

Takes a SCALAR argument, which can be either 'priv' or 'schedule', followed by a PARAMHASH which can have one or more of the properties 'eid', 'nick', and 'tsrange'.

At least one of { 'eid', 'nick' } must be specified. If both are specified, the employee is determined according to 'eid'.

The function returns the history of privilege level or schedule changes for that employee over the given tsrange, or the entire history if no tsrange is supplied.

The return value will always be an App::CELL::Status object.

Upon success, the payload will be a reference to an array of history objects. If nothing is found, the array will be empty. If there is a DBI error, the payload will be undefined.

AUTHOR

Nathan Cutler, <presnypreklad@gmail.com>