API paths for Many to Many relationship - api

I have the following entities on an Api:
Organization > OrganizationId (PK), Name, ...
User > UserId (PK), Name, ...
OrganizationEnrolment > (OrganizationId, UserId) (PK), EnromentTypeId
So OrganizationEnrolment has the enrolments of users in organisations.
An user can only have one enrolment per Organization so the PK (OrganizationId, UserId).
I have a few API endpoints like:
GET Organization = GET organizations/{organizationId}
Update Organization = PUT organizations/{organizationId}
Create User = POST users
Delete User = DELETE users/{userId}
The question is:
What should be the API urls to Create, Delete and Update an Organization Enrolment?
I am struggling with it ...

Get Organization Employees: GET organizations/{organizationId}/employees
Attach Employees to Organization: PUT organizations/{organizationId}/employees
Remove Employees from Organization without deleting Employees: DELETE organizations/{organizationId}/employees

Related

Storing user created folders in SQL database

I am a high school student building an app for students in my school to store their grades and help with their organisation overall. In this app the users can create folders and add grades to these folders. I am using a SQL database for storing the grades. My database currently looks something like this:
I have two tables to store the information, the first one is for the subjects like this:
UserID
ID
DisplayName
ParentID
abcd
1
English
null
defg
2
Vocabulary
1
...
...
...
...
Note that the ParentID is either the ID of another folder to create nested folders, or null.
A table for the grades:
UserID
ID
FolderID
Grade
DisplayName
abcd
1
1
A
Grammar Test
defg
2
2
B
Vocabulary pages 12-13
...
...
...
...
...
Here the FolderID refers to a folder from the table above.
As well as a table for the users:
UserID
FirstName
LastName
Email
Password
abcd
Alice
Smith
alice.smith#example.com
$$
defg
Bob
Brown
bob.brown#example.com
$$
...
...
...
...
...
Upon creating an account the user can decide to either chose a configuration template or skip this step. When he chooses a template he is prompted to choose his subjects from a few options (for example you can choose the second language between German and Italian). And these folders then get created and added to the database.
I now want to store the choices the user made to do some data analysis with it. The problem is that I want the user to be able to edit the folder name if he wants to... A nice bonus would be if I could translate the folders using i18n.
I have thought about creating a new table with all possible subjects, each getting a new id which is a prime number so I can multiply them to store the users unique configuration as well as creating a new column in the table for the SubjectID in the tables and query all the folders from a user. Would this be a good idea?
Do you have any other suggestions for me? (this is my first project of this size and I am happy for all feedback)
I would change the approach. I would have a table for subjects of the format of:
subjects(id, parentID, displayName)
and there would be a many-to-many relation for subject-user, like
user_subjects(id, userID, subjectID, displayName)
that would represent that a user is learning a given subject. Finally, I would create a custom_subjects table of the format of
custom_subjects(id, subjectID, userID, displayName)
so, if a user has a custom displayName, then that would be used and the value would fall back to the user_subjects's displayName, a common pattern in queries would be the following:
SELECT COALESCE(custom_subjects.displayName, subjects.displayName)
FROM users
JOIN user_subjects
on users.id = user_subjects.userID
JOIN subjects
on user_subjects.subjectID = subjects.id
LEFT JOIN custom_subjects
ON subjects.id = custom_subjects.subjectID AND users.id = custom_subjects.userID
This way you will have a default to fall back to and you could override it for each user. I'm unsure about your intention about i18n, you have provided some information about that, but not enough for me to include ideas about it into my answer.

SQL query to retrieve unique record based on role and email fields

I was trying to retrieve some records based on my requirement. As per the current table, a store can have any number of managers and/or cashiers with their respective email addresses. Now, I am trying to retrieve a new set of records which will have only one manager and/or one cashier per branch with their respective email address (there are no criteria to pick up specific manager cashier)
Current table:
BranchName Role Email
-------------------------------------------------
CA Cashier Cashier1#gmail.com
CA Cashier Cashier2#gmail.com
NY Manager Manager1#gmail.com
NY Manager Manager2#gmail.com
MASS Manager Manager#gmail.com
MASS Manager Massm#gmail.com
Expected output:
BranchName Role Email
------------------------------------------------
CA Cashier Cashier1#gmail.com
NY Manager Manager1#gmail.com
MASS Manager massm#gmail.com
The output is based on the requirement that, branch to Role is one to one mapping whereas Role to branch is one- many ( role and email forms a uniqueness for that particular branch).
I have tried using the below query but I am able to see the duplicate roles for a single branch.
select
BranchName, Email, Role
from
(select
BranchName, Email, Role,
ROW_NUMBER() OVER (PARTITION BY Role ORDER BY Email) AS "C"
from
Store
where
Role IN ('Manager', 'General manager', 'Cashier')
)
where
C = 1;
In my above query, while partitioning by role it only display single record with single branch instead of single role for all branches. It could be because of the condition C=1, if I remove this, I am getting all the records.
I hope, I have provided enough details. Kindly provide some inputs to fix this issue.
Thanks
If you don't have a preference/rule for choosing an email address given a branch name and role, then you just do a simple GROUP BY to get a result:
SELECT BranchName,
Role,
MIN(Email) AS Email
FROM Store
GROUP BY BranchName,
Role

SQL: Employee and Customer are Users, But not all Employees are Users

I'm trying to model a database for a rest server which is accessed via mobile app and web clients. Both Employees and Customers are users of the system. I have previously used composition rather than inheritance to design the database. I have used the design from this answer as such.
User (user_id, fullname, email, username)
Role (role_id, description)
UserRole (user_role_id, user_id,role_id, date_from, date_thru)
Customer (user_id, ...)
Employee (user_id, ...)
The problem is that to have an employee you need to have a user but not all employees are users i.e. I'm currently told to include drivers who are employees but not users of th system. So how can I create such a relationship?
You can have another composition level saying called "Person". So employees and customers are persons and persons could be users.
For the employees who are not user, you can have user_id equal to NULL.

Moqui:Can we create parent and child relation between two users

I just want to create one organization(Pharmacy) and under that organization need a different kinds of user like admin user, inventory user, billing user......etc
In your case, there are two dimensions of data control
by userGroup
by organization
So the solution would be:
create organization ['Phrmaacy1', 'Phrmaacy2']
create person ['Admin1', 'Admin2', 'Inventory1', 'Inventory2'], and userAccount for these persons
create userGroup ['Admin', 'Inventory']
create partyRelationship ['Admin1', 'Inventory1'] as member of ['Phrmaacy1'], and ['Admin2', 'Inventory2'] as member of ['Phrmaacy2']
create userGroupMember to put userAccount of person ['Admin1', 'Admin2'] in userGroup ['Admin'], userAccount of person ['inventory1', 'inventory2'] in userGroup ['Inventory']
Use both organization and userGroup the user belong to to filter out data, and use userGroup (userGroupPermissions, artifactAuthz) to control permission the user is allowed to perform.

Database schema: how to implement feedback from users?

I have table 'users'. Users can have roles - 'client' and 'employee'. Clients can give employees feedback (rating). I am not sure, how to implement it correctly, since clients and employees are one entity in schema (users).
I thought about something like this:
So, if a user has role 'client' then he will have feedback through 'given_feedback', and if 'employee', then through 'received_feedback'? Is it appropriate way to do it?
It is alike to this question: Database Design - Linking two users though in my case it is important to know who gave the feedback and who received it.
I would do something like this:
users
id
first_name
last_name
email
role_id
etc..
feedback_users
id
user_id
given_feedback - a value that tells if it's given (1) or recieved feedback (0))
feedback_text
roles
id
name
(two rows with client and employee)
Relations between above tables:
users.id - feedback_users.id (1 to many)
users.role_id - roles.id (many to 1) //given that a user can only have one role