Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create User DB Layer for v9 Migration #12110

Merged
merged 18 commits into from
Feb 24, 2023

Conversation

domlimm
Copy link
Contributor

@domlimm domlimm commented Feb 20, 2023

Part of #12048.

Outline of Solution
The User DB layer supports CRUD ops for both Instructor and Student entities.

To add tests in the future.

@domlimm domlimm force-pushed the v9-user-db-layer branch 4 times, most recently from 4446e09 to f01e51e Compare February 22, 2023 12:49
src/main/java/teammates/storage/sqlentity/User.java Outdated Show resolved Hide resolved
src/main/java/teammates/storage/sqlapi/UserDb.java Outdated Show resolved Hide resolved
src/main/java/teammates/storage/sqlapi/UserDb.java Outdated Show resolved Hide resolved
src/main/java/teammates/storage/sqlapi/UserDb.java Outdated Show resolved Hide resolved
src/main/java/teammates/storage/sqlentity/Instructor.java Outdated Show resolved Hide resolved
Comment on lines 161 to 170
/**
* Checks if a user exists by its {@code id}.
*/
private boolean doesUserExist(Integer id) {
assert id != null;

User user = HibernateUtil.getSessionFactory().getCurrentSession().get(User.class, id);

return user != null;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

String courseId = user.getCourse().getId();
String email = user.getEmail();

if (doesUserExist(courseId, email)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the issue here, will need to split this method back into instructor/student. Validation for both would be different, since course/email is not unique for user.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment samuel! By splitting back into instructor/student does that mean reverting back to something like this?

        // This logic is definitely wrong 😓 
        if (getInstructor(user.getId()) == null) {
            throw new EntityAlreadyExistsException(String.format(ERROR_CREATE_ENTITY_ALREADY_EXISTS, user.toString()));
        }

        if (getStudent(user.getId()) == null) {
            throw new EntityAlreadyExistsException(String.format(ERROR_CREATE_ENTITY_ALREADY_EXISTS, user.toString()));
        }

I checked on the EntitiesDb hasExisting methods in both Instructor and Student:
Instructor
image

generateId in Instructor
image

Student
image

generateId in Student
image

Do I follow these above? If I am, may I ask how should I go about doing it for Student? Noted that course + email is not unique for user too, for this new implementation it would be id itself?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, split createUser into createInstructor and createStudent. Validate existence separately for both.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, thanks samuel!!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last thing, you mentioned that validation will be different for both, are we not using courseId and email anymore? How should I do the validation? A little lost on this part

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, but need hasExistngInstructor and hasExistingStudent. courseId and email is only unique for instructor/student, not for users in general.

Copy link
Contributor Author

@domlimm domlimm Feb 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved! Thanks samuel for clarifying! Hope it's good now!

src/main/java/teammates/storage/sqlapi/UserDb.java Outdated Show resolved Hide resolved
src/main/java/teammates/storage/sqlapi/UserDb.java Outdated Show resolved Hide resolved
src/main/java/teammates/storage/sqlapi/UserDb.java Outdated Show resolved Hide resolved
src/main/java/teammates/storage/sqlapi/UserDb.java Outdated Show resolved Hide resolved
src/main/java/teammates/storage/sqlapi/UserDb.java Outdated Show resolved Hide resolved
src/main/java/teammates/storage/sqlapi/UserDb.java Outdated Show resolved Hide resolved
src/main/java/teammates/storage/sqlapi/UserDb.java Outdated Show resolved Hide resolved
Copy link
Member

@samuelfangjw samuelfangjw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the branch and made some minor tweaks, feel free to have a look.

@samuelfangjw samuelfangjw merged commit 369ff19 into TEAMMATES:v9-migration Feb 24, 2023
@domlimm
Copy link
Contributor Author

domlimm commented Feb 25, 2023

@samuelfangjw Hey samuel, noted on the changes! Thank you!

For the hasExistingT i was thinking maybe getting the count of it is less expensive compared to retrieving the entire row in terms of the amount of data sent back to us. Not too sure but that was the motivation behind it!

Thanks again! 🍻

@wkurniawan07 wkurniawan07 added s.ToMerge The PR is approved by all reviewers including final reviewer; ready for merging c.Task Other non-user-facing works, e.g. refactoring, adding tests labels Jan 21, 2024
@wkurniawan07 wkurniawan07 added this to the V9.0.0-beta.0 milestone Jan 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c.Task Other non-user-facing works, e.g. refactoring, adding tests s.ToMerge The PR is approved by all reviewers including final reviewer; ready for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants