ucsb-cs156-w22 / team04-w22-5pm-courses

1 stars 0 forks source link

PersonalSchedule Repositories - Create Database Table for PersonalSchedules #19

Open ScottUCSB opened 2 years ago

ScottUCSB commented 2 years ago

Create database table for PersonalSchedules

Acceptance Criteria:

Details: @Entity class

The @Entity class should be similar to the file Todo.java in the example repo https://github.com/ucsb-cs156-w22/demo-spring-react-example-v2.

You may also want to consult User.java in the same repo for another example.

Your file will be called PersonalSchedules.java and should be placed in the entities subdirectory of the repo.

It will have a field for id, which should be defined like this:

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long id;

And fields for name,description, and quarter. IMPORTANT NOTE FROM NICK - I'm not sure if 'quarter' should be a String or an int, so unless you are sure, you should double check.

  private String name;
  private String description;
  private String quarter;

Also note that the line:

@Entity(name = "todos")

should be replaced with this, since Spring Boot requires CamelCase while SQL requires snake_case:

@Entity(name = "personal_schedules")

The rest, you should be able to figure out on your own.

Note:

@ManyToOne
@JoinColumn(name = "user_id")

This preceeds the attribute:

  private User user;

which is how we indicate that a particular record belongs to a particular User.

Details: @Repository class

For the repository class, see the example TodoRepository.java.

You may also consult UserRepository.java in the same repo for another example.

Looking at the sample code, let's figure out what needs to be changed:

@Repository
public interface TodoRepository extends CrudRepository<Todo, Long> {
  Iterable<Todo> findAllByUserId(Long user_id);
}
  1. You'll need to change the name TodoRepository to PersonalScheduleRepository
  2. You'll need to change Todo to PersonalSchedule. This Todo refers back to the @Entity class for a single row in the database.
  3. You'll need to replace the method:

    Iterable<Todo> findAllByUserId(Long user_id);

    With these:

    Iterable<PersonalSchedule> findByName(String name);
    Iterable<PersonalSchedule> findByDescription(String quarter);

    Note that one of the suprising things about Spring Boot is that you do not need to write code for these methods. Instead, the part of the Spring Boot framework known as Spring Data JDBC looks at the name you choose for these methods and based on that, it writes them for you.

    The rules for translating method naming conventions into generated code are complicated: we will not go over all of them in lecture, and you are not expected to memorize or learn them all. Instead, you need to be able to find what you need, when you need it.

    Some documentation is here to help get you started: https://docs.spring.io/spring-data/jdbc/docs/current/reference/html/#jdbc.query-methods

    While in this case, we are telling you what methods to add, in future cases, you'll be expected to figure out from context what kinds of lookup methods you might need for a table, consult the Spring Data

tiny-babies commented 2 years ago

NOTE: Issue https://github.com/ucsb-cs156-w22/team04-w22-5pm-courses/issues/8 was created and started before this, and may be equivalent.