Skip to content

Commit

Permalink
Merge pull request #6 from BloomTech-Labs/BL-904-bd-migrate-and-seed-…
Browse files Browse the repository at this point in the history
…newsfeed-table

Bl 904 bd migrate and seed newsfeed table
  • Loading branch information
ashtilawat23 authored Nov 7, 2022
2 parents c831e8c + a62ef1f commit 54c0845
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.bloomtechlabs.coderheroesbea.entities;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "newsfeed")
public class Newsfeed {
// newsfeed_id integer NOT NULL DEFAULT nextval('newsfeed_newsfeed_id_seq'::regclass),
// title character varying(255) COLLATE pg_catalog."default" NOT NULL,
// link character varying(255) COLLATE pg_catalog."default" NOT NULL,
// description text COLLATE pg_catalog."default" NOT NULL,
// posted_at timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
private int newsfeed_id;
private String title;
private String link;
private String description;
private Date posted_at;

/**
* Constructor.
* Default constructor is required to have Hibernate initialize the entity.
*/
public Newsfeed() {

}

/**
* Constructor with newsfeed_id
* @param newsfeed_id - primary key, auto-increments, generated by database
*/
public Newsfeed(int newsfeed_id) {
this.newsfeed_id = newsfeed_id;
}

/**
* Constructor with all fields.
* @param newsfeed_id - primary key, auto-increments, generated by database
* @param title - string not null
* @param link - string not null
* @param description - string not null
* @param posted_at - timestamp (autogenerated)
*/
public Newsfeed(int newsfeed_id, String title, String link, String description, Date posted_at) {
this.newsfeed_id = newsfeed_id;
this.title = title;
this.link = link;
this.description = description;
this.posted_at = posted_at;
}
@Id
@Column(name = "newsfeed_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getNewsfeed_id() {
return newsfeed_id;
}

public void setNewsfeed_id(int newsfeed_id) {
this.newsfeed_id = newsfeed_id;
}

public String getTitle() {
return title;
}
@Column(name = "title", nullable = false)
public void setTitle(String title) {
this.title = title;
}
@Column(name = "link", nullable = false)
public String getLink() {
return link;
}

public void setLink(String link) {
this.link = link;
}
@Column(name = "description", nullable = false)
public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
@Column(name = "posted_at", columnDefinition = "timestamp default CURRENT_TIMESTAMP")
public Date getPosted_at() {
return posted_at;
}

public void setPosted_at(Date posted_at) {
this.posted_at = posted_at;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.bloomtechlabs.coderheroesbea.repositories;

import com.bloomtechlabs.coderheroesbea.entities.Newsfeed;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface NewsfeedRepository extends JpaRepository<Newsfeed,Long> {
}
16 changes: 16 additions & 0 deletions src/main/resources/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,19 @@ insert into courses (course_name, course_description, days_of_week, max_size, en
values ('Mindful Design', 'Students will learn about creativity and web design basics',
'{"Wednesday", "Friday"}', 12, 0, 6, 10, 2, 3, '15:30:00', '17:45:00', '04/04/2022', '04/28/2022', 8, 'Medium', '1-on-1',
'https://docs.google.com');

-- SEED newsfeed

insert into newsfeed (title, link, description)
values('Check out these coding camps!', 'https://www.idtech.com/', 'This is great way to learn more about the coding world!');

insert into newsfeed (title, link, description)
values('Always check the docs!', 'https://developer.mozilla.org/en-US/',
'Make sure to check the documentations if you are looking for a method to use and are not sure what the precise name is!');

insert into newsfeed (title, link, description)
values('Practice your algorithms!', 'https://leetcode.com/', 'Sign up and test your problem-solving skills!');

insert into newsfeed (title, link, description)
values('Robot dog learns to walk in one hour', 'https://www.sciencedaily.com/releases/2022/07/220718122229.htm',
'Like a newborn animal, a four-legged robot stumbles around during its first walking attempts. But while a foal or a giraffe needs much longer to master walking, the robot learns to move forward fluently in just one hour. A computer program acts as the artificial presentation of the animal''s spinal cord, and learns to optimize the robot''s movement in a short time. The artificial neural network is not yet ideally adjusted at the beginning, but rapidly self-adjusts.');
16 changes: 16 additions & 0 deletions src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,20 @@ CREATE TABLE IF NOT EXISTS courses
REFERENCES programs (program_id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE CASCADE
);

-- MIGRATION TABLE newsfeed
DROP TABLE IF EXISTS newsfeed CASCADE;
DROP SEQUENCE IF EXISTS newsfeed_newsfeed_id_seq;
CREATE SEQUENCE IF NOT EXISTS newsfeed_newsfeed_id_seq
start 1
increment 1;
CREATE TABLE IF NOT EXISTS newsfeed
(
newsfeed_id integer NOT NULL DEFAULT nextval('newsfeed_newsfeed_id_seq'::regclass),
title character varying(255) COLLATE pg_catalog."default" NOT NULL,
link character varying(255) COLLATE pg_catalog."default" NOT NULL,
description text COLLATE pg_catalog."default" NOT NULL,
posted_at timestamp with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT newsfeed_pkey PRIMARY KEY (newsfeed_id)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.bloomtechlabs.coderheroesbea.migrationseed;

import com.bloomtechlabs.coderheroesbea.entities.Admins;
import com.bloomtechlabs.coderheroesbea.entities.Newsfeed;
import com.bloomtechlabs.coderheroesbea.entities.Profiles;
import com.bloomtechlabs.coderheroesbea.repositories.AdminsRepository;
import com.bloomtechlabs.coderheroesbea.repositories.NewsfeedRepository;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

@ExtendWith(SpringExtension.class)
@SpringBootTest
public class MigrationSeedNewsfeedTest {
@Autowired
NewsfeedRepository newsfeedRepository;

@Test
public void findAllData_thenAllAdminsCorrect() {
// Given
List<Newsfeed> newsfeeds = List.of(
new Newsfeed(1, "Check out these coding camps!", "https://www.idtech.com/",
"This is great way to learn more about the coding world!", new Date()),
new Newsfeed(2, "Always check the docs!", "https://developer.mozilla.org/en-US/",
"Make sure to check the documentations if you are looking for a method to use and are not sure what the precise name is!",
new Date()),
new Newsfeed(3, "Practice your algorithms!", "https://leetcode.com/",
"Sign up and test your problem-solving skills!", new Date()),
new Newsfeed(4, "Robot dog learns to walk in one hour", "https://www.sciencedaily.com/releases/2022/07/220718122229.htm",
"Like a newborn animal, a four-legged robot stumbles around during its first walking attempts. But while a foal or a giraffe needs much longer to master walking, the robot learns to move forward fluently in just one hour. A computer program acts as the artificial presentation of the animal's spinal cord, and learns to optimize the robot's movement in a short time. The artificial neural network is not yet ideally adjusted at the beginning, but rapidly self-adjusts.",
new Date())
);

// When
List<Newsfeed> foundNewsfeeds = newsfeedRepository.findAll();

// Then
assertEquals(newsfeeds.size(), foundNewsfeeds.size());
for (int i = 0; i < newsfeeds.size(); i++) {
assertTrue(compare(newsfeeds.get(i), foundNewsfeeds.get(i)));
}

}

private boolean compare(Newsfeed newsfeeds, Newsfeed foundNewsfeeds) {
return newsfeeds.getNewsfeed_id() == foundNewsfeeds.getNewsfeed_id()
&& Objects.equals(newsfeeds.getTitle(), foundNewsfeeds.getTitle())
&& Objects.equals(newsfeeds.getLink(), foundNewsfeeds.getLink())
&& Objects.equals(newsfeeds.getDescription(), foundNewsfeeds.getDescription())
;
}
}

0 comments on commit 54c0845

Please sign in to comment.