This is a Makers Vine. Vines are designed to gradually build up sophisticated skills. They contain a mixture of text and video, and may contain some challenge exercises without proposed solutions. Read more about how to use Makers Vines.
Learn to test-drive "Repository" class methods to INSERT, DELETE and UPDATE.
Here's a video demonstration of designing the create, delete and update methods.
The process to follow is the same you used for the previous methods — but the operation we want to implement is to insert new records.
Here is a design for the create method of the example class StudentRepository:
| Method | Job | Arguments | SQL query | Returns |
|---|---|---|---|---|
create |
Insert a student | A Student object |
INSERT INTO students (name, cohort_name) VALUES ($1, $2); |
- |
As the INSERT query contains some dynamic parts (the new values to insert), we need to use SQL parameters as well.
INSERT INTO students (name, cohort_name) VALUES ($1, $2);Here is the expected behaviour for the method:
repository = StudentRepository.new
# Build a new model object
student = Student.new
student.name = 'Alice'
student.cohort_name = 'February 2022'
repository.create(student) # Performs the INSERT query
# Performs a SELECT query to get all records (implemented previously)
all_students = repository.all
# all_students should contain the student 'Alice' created above.The design of the delete method is similar to the find method previously implemented — taking an ID as an argument. However, it runs a DELETE SQL query to delete a specific row.
| Method | Job | Arguments | SQL query | Returns |
|---|---|---|---|---|
delete |
Delete a student | A (numeric) id |
DELETE FROM students WHERE id = $1; |
- |
Here is the expected behaviour for the method:
repository = StudentRepository.new
# Delete the student with id=1
repository.delete(1)
repository.all # The student with id=1 should now be removed.Work in the project directory music_library you've worked on before.
Test-drive the method .create on the class AlbumRepository from the previous project
music_library.
repository = AlbumRepository.new
album = Album.new
album.title = 'Trompe le Monde'
album.release_year = 1991
album.artist_id = 1
repository.create(album)
all_albums = repository.all
# The all_albums array should contain the new Album object- Encode the expected behaviour above as a new test for the
AlbumRepositoryclass. - Implement the
.createmethod to make this test pass.
This is a process feedback challenge. That means you should record yourself doing it and submit that recording to your coach for feedback. How do I do this?
This is a big one! To work on this challenge, first:
- Setup a new project directory
social_network. - Create a new database
social_network.
Then:
-
Design the two tables for the following user stories.
As a social network user, So I can have my information registered, I'd like to have a user account with my email address. As a social network user, So I can have my information registered, I'd like to have a user account with my username. As a social network user, So I can write on my timeline, I'd like to create posts associated with my user account. As a social network user, So I can write on my timeline, I'd like each of my posts to have a title and a content. As a social network user, So I can know who reads my posts, I'd like each of my posts to have a number of views. -
Test-drive the Model and Repository classes for these two tables.
- You should end up with two Model classes and two Repository classes.
- You should test-drive and implement the four methods
all,find,createanddeletefor each Repository class.
-
If you'd like an extra challenge, test-drive as well an
updatemethod for both classes, which updates a specific record.
After you're done, submit your recording here.