mentiflectax / 50web

Sinatra websites using a50c gem
0 stars 0 forks source link

Sign-up: User type is not saved #5

Closed mentiflectax closed 8 years ago

mentiflectax commented 8 years ago

Next steps:

  1. Create a table in the songcontest schema to save these data. Reference to peeps (table with the data about people).
  2. Modify the route signup so that the user type is saved in the table from previous step.
mentiflectax commented 8 years ago

Branch for this issue is issue5.

mentiflectax commented 8 years ago

Note that we have these branches in both 50web and db-api projects.

mentiflectax commented 8 years ago

We have several options to store user type (fan, musician):

  1. In a separate table like peeps.urls.
  2. Use peeps.attributes table.
CREATE TABLE peeps.attributes (
    person_id integer NOT NULL REFERENCES peeps.people(id) ON DELETE CASCADE,
    attribute varchar(16) NOT NULL REFERENCES peeps.atkeys(atkey),
    plusminus boolean NOT NULL,  -- true if yes, false if no
    PRIMARY KEY (person_id, attribute)
);

For now, I select the second option for two reasons:

  1. Use an already existing entity.
  2. It allows to model a situation (in future), where a person is both a fan and a musician.
mentiflectax commented 8 years ago

Next question: Isn't there an API function, which allows to set and unset attributes (do we have to write it ourselves, or can we re-use something existing) ?

mentiflectax commented 8 years ago

There is a an existing function:

-- PARAMS: person_id, attribute, plusminus
CREATE OR REPLACE FUNCTION peeps.person_set_attribute(integer, text, boolean,
    OUT status smallint, OUT js json) AS $$
DECLARE
m4_ERRVARS
BEGIN
    UPDATE peeps.attributes SET plusminus=$3 WHERE person_id=$1 AND attribute=$2;
    IF NOT FOUND THEN
        INSERT INTO peeps.attributes VALUES ($1, $2, $3);
    END IF;
    SELECT x.status, x.js INTO status, js FROM peeps.person_attributes($1) x;
m4_ERRCATCH
END;
$$ LANGUAGE plpgsql;
mentiflectax commented 8 years ago

In order for this to work, we need to insert fan and musician attributes during setup. See the schema file in the mvp branch of db-api project.

mentiflectax commented 8 years ago

Merged the fix into the mvp branches of both projects: