Skip to content

Commit

Permalink
Added WidgetRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
adarshreddy87 committed Apr 2, 2021
1 parent aff8b40 commit b861aec
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 35 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
package com.example.CS5610Sp2101kadarshreddyserverjava.models;

import javax.persistence.*;

@Entity
@Table(name = "widgets")
public class Widget {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String topicId;
private String type;
private Integer size;
private String text;
private Integer height;
private Integer width;
private String name;

public Integer getHeight() {
return height;
}

public void setHeight(Integer height) {
this.height = height;
}

public Integer getWidth() {
return width;
}

public void setWidth(Integer width) {
this.width = width;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Long getId() {
return id;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.CS5610Sp2101kadarshreddyserverjava.repositories;

import com.example.CS5610Sp2101kadarshreddyserverjava.models.Widget;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface WidgetRepository extends CrudRepository<Widget,Long> {

@Query(value = "select * from widgets", nativeQuery = true)
public List<Widget> findAllWidgets();

@Query(value = "select * from widgets where id=:wid", nativeQuery = true)
public Widget findWidgetsById(@Param("wid") Long widgetId);

@Query(value = "select * from widgets where topic_id=:tid", nativeQuery = true)
public List<Widget> findWidgetsForTopic(@Param("tid") String topicId);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.example.CS5610Sp2101kadarshreddyserverjava.services;

import com.example.CS5610Sp2101kadarshreddyserverjava.models.Widget;
import com.example.CS5610Sp2101kadarshreddyserverjava.repositories.WidgetRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
Expand All @@ -9,6 +11,10 @@

@Service
public class WidgetService {

@Autowired
WidgetRepository repository;

private List<Widget> widgets = new ArrayList<Widget>();
{
Widget w1 = new Widget(123l, "ABC123", "HEADING", 1, "Widgets for Topic ABC123");
Expand All @@ -25,56 +31,69 @@ public class WidgetService {
}

public Widget createWidgetForTopic(Widget widget){
Long id = (new Date()).getTime();
widget.setId(id);
widgets.add(widget);
return widget;
return repository.save(widget);
// Long id = (new Date()).getTime();
// widget.setId(id);
// widgets.add(widget);
// return widget;
}

public List<Widget> findAllWidgets(){
return widgets;
return repository.findAllWidgets();
// return (List<Widget>) repository.findAll();
// return widgets;
}

public List<Widget> findWidgetsForTopic(String topicId){
List<Widget> ws = new ArrayList<Widget>();
for(Widget w:widgets){
if (w.getTopicId().equals(topicId)){
ws.add(w);
}
}
return ws;
return repository.findWidgetsForTopic(topicId);
// List<Widget> ws = new ArrayList<Widget>();
// for(Widget w:widgets){
// if (w.getTopicId().equals(topicId)){
// ws.add(w);
// }
//
// }
// return ws;
}

public Widget findWidgetById(Long id) {
for(Widget w: widgets) {
if(w.getId().equals(id)) {
return w;
}
}
return null;
// for(Widget w: widgets) {
// if(w.getId().equals(id)) {
// return w;
// }
// }
// return null;
// return repository.findById(id).get();
return repository.findWidgetsById(id);
}

public Integer deleteWidget(Long id){
int index=-1;
for(int i=0;i<widgets.size();i++){
if(widgets.get(i).getId().equals(id)){
index=i;
}
}
if(index>=0){
widgets.remove(index);
repository.deleteById(id);
return 1;
}
return -1;
// int index=-1;
// for(int i=0;i<widgets.size();i++){
// if(widgets.get(i).getId().equals(id)){
// index=i;
// }
// }
// if(index>=0){
// widgets.remove(index);
// return 1;
// }
// return -1;
}

public Integer updateWidget(Long id, Widget widget){
for (int i =0 ;i<widgets.size();i++){
if(widgets.get(i).getId().equals(id)){
widgets.set(i,widget);
return 1;
}
}
return -1;
Widget originalWidget = findWidgetById(id);
originalWidget.setText(widget.getText());
repository.save(originalWidget);
return 1;
// for (int i =0 ;i<widgets.size();i++){
// if(widgets.get(i).getId().equals(id)){
// widgets.set(i,widget);
// return 1;
// }
// }
// return -1;
}
}
13 changes: 13 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@


spring.datasource.url=jdbc:mysql://localhost:3306/wbdv_schema?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=Adarsh123
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.type=trace

0 comments on commit b861aec

Please sign in to comment.