diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 0f0ea20..961d96c 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Sun Oct 01 18:38:57 EEST 2017 +#Fri Jan 26 16:32:35 EET 2018 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.5.1-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-bin.zip diff --git a/gradlew b/gradlew index 4453cce..cccdd3d 100755 --- a/gradlew +++ b/gradlew @@ -33,11 +33,11 @@ DEFAULT_JVM_OPTS="" # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD="maximum" -warn ( ) { +warn () { echo "$*" } -die ( ) { +die () { echo echo "$*" echo @@ -155,7 +155,7 @@ if $cygwin ; then fi # Escape application args -save ( ) { +save () { for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done echo " " } diff --git a/gradlew.bat b/gradlew.bat index f955316..e95643d 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -1,84 +1,84 @@ -@if "%DEBUG%" == "" @echo off -@rem ########################################################################## -@rem -@rem Gradle startup script for Windows -@rem -@rem ########################################################################## - -@rem Set local scope for the variables with windows NT shell -if "%OS%"=="Windows_NT" setlocal - -set DIRNAME=%~dp0 -if "%DIRNAME%" == "" set DIRNAME=. -set APP_BASE_NAME=%~n0 -set APP_HOME=%DIRNAME% - -@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. -set DEFAULT_JVM_OPTS= - -@rem Find java.exe -if defined JAVA_HOME goto findJavaFromJavaHome - -set JAVA_EXE=java.exe -%JAVA_EXE% -version >NUL 2>&1 -if "%ERRORLEVEL%" == "0" goto init - -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:findJavaFromJavaHome -set JAVA_HOME=%JAVA_HOME:"=% -set JAVA_EXE=%JAVA_HOME%/bin/java.exe - -if exist "%JAVA_EXE%" goto init - -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. - -goto fail - -:init -@rem Get command-line arguments, handling Windows variants - -if not "%OS%" == "Windows_NT" goto win9xME_args - -:win9xME_args -@rem Slurp the command line arguments. -set CMD_LINE_ARGS= -set _SKIP=2 - -:win9xME_args_slurp -if "x%~1" == "x" goto execute - -set CMD_LINE_ARGS=%* - -:execute -@rem Setup the command line - -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar - -@rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% - -:end -@rem End local scope for the variables with windows NT shell -if "%ERRORLEVEL%"=="0" goto mainEnd - -:fail -rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of -rem the _cmd.exe /c_ return code! -if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 -exit /b 1 - -:mainEnd -if "%OS%"=="Windows_NT" endlocal - -:omega +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/out/production/resources/application.yml b/out/production/resources/application.yml new file mode 100644 index 0000000..f5d4db0 --- /dev/null +++ b/out/production/resources/application.yml @@ -0,0 +1,2 @@ +application: + base-url: "http://localhost:8080/api" \ No newline at end of file diff --git a/out/production/resources/templates/index.html b/out/production/resources/templates/index.html new file mode 100644 index 0000000..cc09721 --- /dev/null +++ b/out/production/resources/templates/index.html @@ -0,0 +1,50 @@ + + + + + Spring Boot - POST-GET AJAX Example + + + + + + +
+ +
+
+ + +
+ +
+ + + + + + +
+ + + + + + \ No newline at end of file diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 0000000..1fef387 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,2 @@ +include 'uniBlog' + diff --git a/src/main/java/ro/ubb/istudent/controller/Comments.java b/src/main/java/ro/ubb/istudent/controller/Comments.java new file mode 100644 index 0000000..934c333 --- /dev/null +++ b/src/main/java/ro/ubb/istudent/controller/Comments.java @@ -0,0 +1,15 @@ +package ro.ubb.istudent.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +/** + * Created by catablack. + */ +@Controller +public class Comments { + + @RequestMapping(value="/news/comments/{Id}",method= RequestMethod.GET) + public String news() {return "newsPage";} +} diff --git a/src/main/java/ro/ubb/istudent/controller/GreetingController.java b/src/main/java/ro/ubb/istudent/controller/GreetingController.java index 5122ab4..ef81712 100644 --- a/src/main/java/ro/ubb/istudent/controller/GreetingController.java +++ b/src/main/java/ro/ubb/istudent/controller/GreetingController.java @@ -12,4 +12,5 @@ public String home() { return "index"; } + } \ No newline at end of file diff --git a/src/main/java/ro/ubb/istudent/controller/NewController.java b/src/main/java/ro/ubb/istudent/controller/NewController.java new file mode 100644 index 0000000..1b6885b --- /dev/null +++ b/src/main/java/ro/ubb/istudent/controller/NewController.java @@ -0,0 +1,37 @@ +package ro.ubb.istudent.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by catablack. + */ +@Controller +public class NewController { + +// @RequestMapping(value = "/news", method = RequestMethod.GET) +// public String news() { +// +// return "news"; +// } + + /* + @RequestMapping(path = "/news", method = RequestMethod.GET) + public @ResponseBody + Map sayHello() { + + Map stringStringMap= new HashMap<>(); + stringStringMap.put("Hello","World"); + return stringStringMap; + } + */ + @RequestMapping(value="/news",method=RequestMethod.GET) + public String news() {return "news";} + + +} diff --git a/src/main/java/ro/ubb/istudent/domain/New.java b/src/main/java/ro/ubb/istudent/domain/New.java new file mode 100644 index 0000000..e26f574 --- /dev/null +++ b/src/main/java/ro/ubb/istudent/domain/New.java @@ -0,0 +1,83 @@ +package ro.ubb.istudent.domain; + + +import org.bson.types.ObjectId; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +import java.io.Serializable; +import java.util.Objects; + +/** + * Created by catablack. + */ + +@Document(collection = "new") +public class New implements Serializable { + @Id + private ObjectId id; + + private String message; + + private String title; + + private String teacher; + + private String course; + + public ObjectId getId() { + return id; + } + + public void setId(ObjectId id) { + this.id = id; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getTeacher() { + return teacher; + } + + public void setTeacher(String teacher) { + this.teacher = teacher; + } + + public String getCourse() { + return course; + } + + public void setCourse(String course) { + this.course = course; + } + + @Override + public boolean equals(Object o) { + + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + New aNew = (New) o; + return Objects.equals(id, aNew.id) && + Objects.equals(message, aNew.message); + } + + @Override + public int hashCode() { + + return Objects.hash(id, message); + } +} diff --git a/src/main/java/ro/ubb/istudent/domain/NewsComments.java b/src/main/java/ro/ubb/istudent/domain/NewsComments.java new file mode 100644 index 0000000..c6a9d6e --- /dev/null +++ b/src/main/java/ro/ubb/istudent/domain/NewsComments.java @@ -0,0 +1,68 @@ +package ro.ubb.istudent.domain; + +import org.bson.types.ObjectId; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + +import java.util.Objects; + +/** + * Created by catablack. + */ +@Document(collection = "newComments") +public class NewsComments { + @Id + private ObjectId id; + private String newsId; + private String message; + private String posterName; + + public ObjectId getId() { + return id; + } + + public void setId(ObjectId id) { + this.id = id; + } + + public String getNewsId() { + return newsId; + } + + public void setNewsId(String newsId) { + this.newsId = newsId; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getPosterName() { + return posterName; + } + + public void setPosterName(String posterName) { + this.posterName = posterName; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + NewsComments that = (NewsComments) o; + return Objects.equals(id, that.id) && + Objects.equals(newsId, that.newsId) && + Objects.equals(message, that.message) && + Objects.equals(posterName, that.posterName); + } + + @Override + public int hashCode() { + + return Objects.hash(id, newsId, message, posterName); + } +} diff --git a/src/main/java/ro/ubb/istudent/dto/NewDto.java b/src/main/java/ro/ubb/istudent/dto/NewDto.java new file mode 100644 index 0000000..c70e9af --- /dev/null +++ b/src/main/java/ro/ubb/istudent/dto/NewDto.java @@ -0,0 +1,54 @@ +package ro.ubb.istudent.dto; + + +/** + * Created by catablack. + */ +public class NewDto implements Dto { + + String id; + String message; + String title; + String teacher; + String course; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getTeacher() { + return teacher; + } + + public void setTeacher(String teacher) { + this.teacher = teacher; + } + + public String getCourse() { + return course; + } + + public void setCourse(String course) { + this.course = course; + } +} diff --git a/src/main/java/ro/ubb/istudent/dto/NewsCommentsDto.java b/src/main/java/ro/ubb/istudent/dto/NewsCommentsDto.java new file mode 100644 index 0000000..d29fd90 --- /dev/null +++ b/src/main/java/ro/ubb/istudent/dto/NewsCommentsDto.java @@ -0,0 +1,44 @@ +package ro.ubb.istudent.dto; + + +/** + * Created by catablack. + */ +public class NewsCommentsDto implements Dto { + private String id; + private String newsId; + private String message; + private String posterName; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getNewsId() { + return newsId; + } + + public void setNewsId(String newsId) { + this.newsId = newsId; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getPosterName() { + return posterName; + } + + public void setPosterName(String posterName) { + this.posterName = posterName; + } +} diff --git a/src/main/java/ro/ubb/istudent/repository/CommentsRepository.java b/src/main/java/ro/ubb/istudent/repository/CommentsRepository.java new file mode 100644 index 0000000..eb011bf --- /dev/null +++ b/src/main/java/ro/ubb/istudent/repository/CommentsRepository.java @@ -0,0 +1,19 @@ +package ro.ubb.istudent.repository; + +import org.bson.types.ObjectId; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; +import ro.ubb.istudent.domain.New; +import ro.ubb.istudent.domain.NewsComments; + +import java.util.List; +import java.util.Optional; + +/** + * Created by catablack. + */ +public interface CommentsRepository extends MongoRepository { + + @Query("{ 'newsId' : ?0 }") + Optional> findNewBynewsId(String newId); +} diff --git a/src/main/java/ro/ubb/istudent/repository/NewRepository.java b/src/main/java/ro/ubb/istudent/repository/NewRepository.java new file mode 100644 index 0000000..13e5985 --- /dev/null +++ b/src/main/java/ro/ubb/istudent/repository/NewRepository.java @@ -0,0 +1,17 @@ +package ro.ubb.istudent.repository; + + +import org.bson.types.ObjectId; +import org.springframework.data.mongodb.repository.MongoRepository; +import ro.ubb.istudent.domain.New; + +import java.util.Optional; + +/** + * Created by catablack. + */ +public interface NewRepository extends MongoRepository { + + Optional findNewById(String newId); + +} diff --git a/src/main/java/ro/ubb/istudent/rest/NewResource.java b/src/main/java/ro/ubb/istudent/rest/NewResource.java new file mode 100644 index 0000000..09d491b --- /dev/null +++ b/src/main/java/ro/ubb/istudent/rest/NewResource.java @@ -0,0 +1,113 @@ +package ro.ubb.istudent.rest; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import ro.ubb.istudent.domain.New; +import ro.ubb.istudent.dto.GreetingDto; +import ro.ubb.istudent.dto.NewDto; +import ro.ubb.istudent.service.GreetingService; +import ro.ubb.istudent.service.NewService; +import ro.ubb.istudent.util.ResponseUtil; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by catablack. + */ +@RequestMapping("/api") +@RestController +public class NewResource { + + + private static final String NEWS_CONTROLLER_MAPPING = "/news"; + private static final Logger LOG = LoggerFactory.getLogger(GreetingResource.class); + private final NewService service; + private final String baseUrl; + + public NewResource(NewService service, @Value("${application.base-url}") String baseUrl) { + this.service = service; + this.baseUrl = baseUrl; + } + + + @GetMapping("/news") + public @ResponseBody + Map> getHelloWorldGreeting() + { + Map> result = new HashMap<>(); + + try{ + + result.put("news",service.findAll().get()); + return result; + } catch (Exception e) + { + // + } + return result; + + } + + @GetMapping("/news/{newsId}") + public @ResponseBody + Map getHelloWorldGreeting(@PathVariable("newsId") String newsId) { + Map result = new HashMap<>(); + + try{ + NewDto newDto = service.findNewById(newsId).get(); + result.put("new",newDto); + return result; + } catch (Exception e) + { + // + } + return result; + } + @GetMapping("/news/getall") + public @ResponseBody + List GetAll(){ + System.out.println("***************-"); + try{ + + List ress= service.GetAll(); + + + return ress; + }catch(Exception e){ + // + } + return null; + } + + + @PostMapping("/news") + public ResponseEntity createGreeting(@RequestBody NewDto newDto) throws URISyntaxException { + LOG.debug("Creating greeting with value: " + newDto); + newDto.setTeacher("Pop Ion"); + NewDto savedNew = service.createNew(newDto); + return ResponseEntity.created(new URI(baseUrl + NEWS_CONTROLLER_MAPPING + "/" + savedNew.getId())).build(); + } + + + @PostMapping("/news/delete/{newsId}") + public ResponseEntity deleteNews(@PathVariable("newsId") String newsId) { + service.Delete(newsId); + return ResponseEntity.ok().build(); + } + + @PostMapping("/news/update/{newsId}") + public ResponseEntity updateNews(@PathVariable("newsId") String newsId, @RequestBody NewDto news) { + LOG.debug("Updating greeting with id: " + newsId + " and new greeting value:" + news); + System.out.println(">>>>>>>>>>>>>>>>>id:"+newsId+"title:"+news.getTitle()); + service.updateNewsWithId(newsId, news); + return ResponseEntity.ok().build(); + } + +} diff --git a/src/main/java/ro/ubb/istudent/rest/NewsCommentsResource.java b/src/main/java/ro/ubb/istudent/rest/NewsCommentsResource.java new file mode 100644 index 0000000..6e48c62 --- /dev/null +++ b/src/main/java/ro/ubb/istudent/rest/NewsCommentsResource.java @@ -0,0 +1,59 @@ +package ro.ubb.istudent.rest; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import ro.ubb.istudent.domain.NewsComments; +import ro.ubb.istudent.dto.NewDto; +import ro.ubb.istudent.dto.NewsCommentsDto; +import ro.ubb.istudent.service.CommentsService; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by catablack. + */ +@RequestMapping("/api") +@RestController +public class NewsCommentsResource { + private static final String NEWS_COMMENTS_CONTROLLER_MAPPING = "/news/comments"; + private static final Logger LOG = LoggerFactory.getLogger(GreetingResource.class); + private final CommentsService service; + private final String baseUrl; + + public NewsCommentsResource(CommentsService service, @Value("${application.base-url}") String baseUrl) { + this.service = service; + this.baseUrl = baseUrl; + } + + @GetMapping("/news/comments/{newId}") + public @ResponseBody + Map> getNewsComments(@PathVariable("newId") String newId) + { + Map> result = new HashMap<>(); + + try{ + + result.put("comments",service.findNewCommentsById(newId).get()); + return result; + } catch (Exception e) + { + + } + return result; + } + + @PostMapping("/news/comments") + public ResponseEntity createComment(@RequestBody NewsCommentsDto newDto) throws URISyntaxException { + LOG.debug("Creating comments with value: " + newDto); + newDto.setPosterName("Pop Ion"); + NewsCommentsDto savedNew = service.createComment(newDto); + return ResponseEntity.ok().build(); + } +} diff --git a/src/main/java/ro/ubb/istudent/service/CommentsService.java b/src/main/java/ro/ubb/istudent/service/CommentsService.java new file mode 100644 index 0000000..26e0b5d --- /dev/null +++ b/src/main/java/ro/ubb/istudent/service/CommentsService.java @@ -0,0 +1,72 @@ +package ro.ubb.istudent.service; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; +import ro.ubb.istudent.domain.NewsComments; +import ro.ubb.istudent.dto.GreetingDto; +import ro.ubb.istudent.dto.NewsCommentsDto; +import ro.ubb.istudent.repository.CommentsRepository; +import ro.ubb.istudent.repository.NewRepository; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +/** + * Created by catablack. + */ +@Service +public class CommentsService { + + private static final Logger LOG = LoggerFactory.getLogger(NewService.class); + private CommentsRepository commentsRepository; + + public CommentsService(CommentsRepository commentsRepository) { + this.commentsRepository = commentsRepository; + } + + public Optional> findNewCommentsById(String newId) { + return commentsRepository.findNewBynewsId(newId) + .map(this::newCommentsToNewsCommentsDTO); + } + + private List newCommentsToNewsCommentsDTO(List newsComments) { + List result = new ArrayList<>(); + for (NewsComments com : newsComments) { + + NewsCommentsDto commentsDto = new NewsCommentsDto(); + commentsDto.setId(com.getId().toString()); + commentsDto.setMessage(com.getMessage()); + commentsDto.setNewsId(com.getNewsId()); + commentsDto.setPosterName(com.getPosterName()); + result.add(commentsDto); + } + return result; + } + + public NewsCommentsDto createComment(NewsCommentsDto newsComment) { + return commentToCommentDTO(commentsRepository.save(commentDTOToEntity(newsComment))); + } + + private NewsCommentsDto commentToCommentDTO(NewsComments newsComments) { + NewsCommentsDto commentsDto = new NewsCommentsDto(); + commentsDto.setMessage(newsComments.getMessage()); + commentsDto.setNewsId(newsComments.getNewsId()); + commentsDto.setPosterName(newsComments.getPosterName()); + return commentsDto; + } + + private NewsComments commentDTOToEntity(NewsCommentsDto newsCommenDto) { + NewsComments newsComments = new NewsComments(); + + newsComments.setMessage(newsCommenDto.getMessage()); + newsComments.setNewsId(newsCommenDto.getNewsId()); + newsComments.setPosterName(newsCommenDto.getPosterName()); + + return newsComments; + + } + + +} diff --git a/src/main/java/ro/ubb/istudent/service/NewService.java b/src/main/java/ro/ubb/istudent/service/NewService.java new file mode 100644 index 0000000..1ab8d3f --- /dev/null +++ b/src/main/java/ro/ubb/istudent/service/NewService.java @@ -0,0 +1,82 @@ +package ro.ubb.istudent.service; + +import org.springframework.stereotype.Service; + +import ro.ubb.istudent.domain.New; +import ro.ubb.istudent.dto.NewDto; +import ro.ubb.istudent.repository.NewRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; + +/** + * Created by catablack. + */ +@Service +public class NewService { + + private static final Logger LOG = LoggerFactory.getLogger(NewService.class); + private NewRepository newRepository; + + public NewService(NewRepository newRepository) { + this.newRepository = newRepository; + } + + public Optional findNewById(String newId) { + return newRepository.findNewById(newId) + .map(this::newToNewDTO); + } + + public Optional> findAll() { + return Optional.of(newRepository.findAll().stream().map(this::newToNewDTO).collect(Collectors.toList())); + } + + private NewDto newToNewDTO(New aNew) { + NewDto newDto = new NewDto(); + newDto.setId(aNew.getId().toHexString()); + newDto.setMessage(aNew.getMessage()); + newDto.setTitle(aNew.getTitle()); + newDto.setTeacher(aNew.getTeacher()); + newDto.setCourse(aNew.getCourse()); + return newDto; + } + + private New newDTOToEntity(NewDto dto) { + New entity = new New(); + entity.setMessage(dto.getMessage()); + entity.setTitle(dto.getTitle()); + entity.setCourse(dto.getCourse()); //linie scrisa de Tudor + entity.setTeacher(dto.getTeacher()); //si asta + return entity; + } + + public NewDto createNew(NewDto newDto) { + return newToNewDTO(newRepository.save(newDTOToEntity(newDto))); + } + + public List GetAll(){return newRepository.findAll().stream().map(this::newToNewDTO).collect(Collectors.toList());} + + public void Delete(String newsId){ + Optional optionalNewEntity = newRepository.findNewById(newsId); + if (optionalNewEntity.isPresent()) { + newRepository.delete(optionalNewEntity.get()); + } else { + LOG.error("News with id {} not found", newsId); + } + } + + public void updateNewsWithId(String newsId, NewDto request) { + Optional optionalNewsEntity = newRepository.findNewById(newsId); + if (optionalNewsEntity.isPresent()) { + optionalNewsEntity.get().setMessage(request.getMessage()); + optionalNewsEntity.get().setCourse(request.getCourse()); + optionalNewsEntity.get().setTitle(request.getTitle()); + newRepository.save(optionalNewsEntity.get()); + } else { + LOG.error("News with id {} not found", newsId); + } + } +} diff --git a/src/main/resources/templates/news.html b/src/main/resources/templates/news.html new file mode 100644 index 0000000..84fe26b --- /dev/null +++ b/src/main/resources/templates/news.html @@ -0,0 +1,89 @@ + + + + + + News blog + + + + + + + + + + +

Welcome to the University blog

+
+
+ + +
+ + +
+ + + +
+
+ + +
+
+ + +
+ +
+ + +
+
+ + +
+ +

+ +
+ + + + + + + + + + + + + + +
Title Description Teacher Course
+
+ +
+ + + + \ No newline at end of file diff --git a/src/main/resources/templates/newsPage.html b/src/main/resources/templates/newsPage.html new file mode 100644 index 0000000..686fb86 --- /dev/null +++ b/src/main/resources/templates/newsPage.html @@ -0,0 +1,93 @@ + + + + + + News + + + + + + + + + + +

Welcome to the University blog

+
+
+ + +
+ + +
+

News details

+ + +
+
+ + +
+
+ + +
+ +
+ + +
+
+
+ + +
+
+
+ + +
+
+ +
+

+ +
+ + + + + + + + + + +
Comment Author
+
+ +
+ + + + \ No newline at end of file diff --git a/src/main/webapp/js/news.js b/src/main/webapp/js/news.js new file mode 100644 index 0000000..e6a1f2a --- /dev/null +++ b/src/main/webapp/js/news.js @@ -0,0 +1,117 @@ +function deleteNews(newid) { + $.ajax({ + type: "POST", + url: "http://localhost:8080/api/news/delete/" + newid, + success: function (data) { + getall(); + }, + error: function (data, status, xhr) { + alert("Error"); + } + }); +} + +function updateNews(id, title, message, course) { + $('#add-btn').hide(); + $('#update-btn').show(); + $('#message').val(message); + $('#title').val(title); + $('#course').val(course); + $('#id-hidden').val(id); + return false; +} + +function getall() { + $("tbody").empty(); + $.ajax({ + type: "GET", + url: "http://localhost:8080/api/news/getall", + success: function (data) { + //alert("aiki"); + console.log(data); + $.each(data, function (index, value) { + var msg = '' + value.message + ''; + var tch = '' + value.teacher + ''; + var crs = '' + value.course + ''; + var trr = ''; + var delClk = 'onclick=\"deleteNews(\'' + value.id + '\')\"'; + //var delClk='onclick=\"fml()\"'; + var updClk = "onclick=\"updateNews('" + value.id + "','" + value.title + "','" + value.message + "','" + value.course + "')\" "; + $("#list").append(trr + '' + value.title + '' + msg + tch + crs + ''); + //alert(value); + }); + }, + error: function (data) { + alert("Error!"); + } + }); + + +} + +$(document).ready(function () { + var url = "http://localhost:8080/api/news/getall"; + $('#update-btn').hide(); + + // alert(url); + + + function postNews() { + //alert('akolo'); + var m = $('#message').val(); + var title = $('#title').val(); + var course = $('#course').val(); + var body = {id: '', message: m, course: course, teacher: "", title: title}; + $.ajax({ + type: "POST", + url: "http://localhost:8080/api/news", + data: JSON.stringify(body), + dataType: "text", + contentType: "application/json; charset=utf-8", + success: function (data) { + //alert("este bine"); + getall(); + }, + error: function (data, status, xhr) { + //var err = eval("(" + xhr.getResponseHeader('Location') + ")"); + alert(err); + } + }); + } + + getall(); + + $("#add-btn").click(function (event) { + event.preventDefault(); + postNews(); + + }); + + + $('#update-btn').click(function (event) { + event.preventDefault(); + + var id = $('#id-hidden').val(); + var m = $('#message').val(); + var course = $('#title').val(); + var title = $('#title').val(); + var body = {id: id, message: m, course: course, teacher: "Pop Ion", title: title}; + $.ajax({ + type: "POST", + url: "http://localhost:8080/api/news/update/" + id, + data: JSON.stringify(body), + contentType: "application/json; charset=utf-8", + success: function (data) { + getall(); + $('#update-btn').hide(); + $('#add-btn').show(); + + }, + error: function (data, status, xhr) { + alert("Error"); + } + }); + }) + + } +); \ No newline at end of file diff --git a/src/main/webapp/js/newsPage.js b/src/main/webapp/js/newsPage.js new file mode 100644 index 0000000..efd05d1 --- /dev/null +++ b/src/main/webapp/js/newsPage.js @@ -0,0 +1,75 @@ +function fillLabels(){ + var url = window.location + ''; + var res = url.split("/"); + var id =res.slice(-1).pop(); + $.ajax({ + type:"GET", + url:"http://localhost:8080/api/news/"+id, + success:function(data){ + console.log(data.new.title); + $('#title').val(data.new.title); + $('#message').val(data.new.message); + $('#course').val(data.new.course); + }, + error:function (data, status, xhr){ + alert("Error"); + } + }) +} + +function loadAll(){ + var url = window.location + ''; + var res = url.split("/"); + $('#list-comm').empty(); + $.ajax({ + type: "GET", + url: "http://localhost:8080/api/news/comments/" + res.slice(-1).pop(), + data: "", + contentType: "application/json; charset=utf-8", + success: function (data) { + console.log(data); + if(data===undefined){ + alert("Undefined"); + } + else { + console.log(data); + if(data.comments.length!=0) { + $.each(data.comments, function (index, value) { + var msg = value.message; + var author = value.posterName; + $('#list-comm').append('' + msg + '' + author + ''); + }); + } + } + + }, + error: function (data, status, xhr) { + alert("Error"); + } + }); +} +$(document).ready(function () { + + loadAll(); + fillLabels(); + $('#add-comm-btn').click(function(event) + { + event.preventDefault(); + var url = window.location + ''; + var res = url.split("/"); + var msg = $('#comm-msg').val(); + var body = {id:'',newsId:res.slice(-1).pop(),message:msg,posterName:'Pop Ion'}; + $.ajax({ + type:"POST", + url:"http://localhost:8080/api/news/comments", + contentType: "application/json; charset=utf-8", + data:JSON.stringify(body), + success:function(data){ + loadAll(); + }, + error:function(data,status,xhr){ + alert("Error"); + } + }) + }) +}); \ No newline at end of file diff --git a/uniBlog/build.gradle b/uniBlog/build.gradle new file mode 100644 index 0000000..8695bad --- /dev/null +++ b/uniBlog/build.gradle @@ -0,0 +1,14 @@ +group 'ro.ubb' +version '0.0.1-SNAPSHOT' + +apply plugin: 'java' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.11' +} diff --git a/uniBlog/src/main/java/ro/ubb/Main.java b/uniBlog/src/main/java/ro/ubb/Main.java new file mode 100644 index 0000000..9bb64bc --- /dev/null +++ b/uniBlog/src/main/java/ro/ubb/Main.java @@ -0,0 +1,16 @@ +package ro.ubb; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Created by tudorstanila on 14/12/2017. + */ + + +@SpringBootApplication +public class Main { + public static void main(String[] args) { + SpringApplication.run(Main.class); + } +} \ No newline at end of file diff --git a/uniBlog/src/main/java/ro/ubb/domain/Course.java b/uniBlog/src/main/java/ro/ubb/domain/Course.java new file mode 100644 index 0000000..2701ae8 --- /dev/null +++ b/uniBlog/src/main/java/ro/ubb/domain/Course.java @@ -0,0 +1,60 @@ +package ro.ubb.domain; + +import java.io.Serializable; +import java.util.List; + +/** + * Created by tudorstanila on 14/12/2017. + */ +public class Course implements Serializable { + private String id; + private String name; + + private Teacher teacher; + private List Students; + + public Course(String id, String name) { + this.id = id; + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Teacher getTeacher() { + return teacher; + } + + public void setTeacher(Teacher teacher) { + this.teacher = teacher; + } + + public List getStudents() { + return Students; + } + + public void setStudents(List students) { + Students = students; + } + + @Override + public String toString() { + return "Course{" + + "id='" + id + '\'' + + ", name='" + name + '\'' + + '}'; + } +} diff --git a/uniBlog/src/main/java/ro/ubb/domain/News.java b/uniBlog/src/main/java/ro/ubb/domain/News.java new file mode 100644 index 0000000..d2b975a --- /dev/null +++ b/uniBlog/src/main/java/ro/ubb/domain/News.java @@ -0,0 +1,48 @@ +package ro.ubb.domain; + +import java.io.Serializable; + +/** + * Created by tudorstanila on 14/12/2017. + */ +public class News implements Serializable { + private int id; + private Person author; + private String text; + + public News(int Id,Person author, String text) { + this.id=Id; + this.author = author; + this.text = text; + } + + public Person getAuthor() { + return author; + } + + public int getId(){return this.id;} + + public void setId(int id){this.id=id;} + + public void setAuthor(Person author) { + this.author = author; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + + @Override + public String toString() { + return "News{" + + "id=" + id + + ", author=" + author + + ", text='" + text + '\'' + + '}'; + } +} diff --git a/uniBlog/src/main/java/ro/ubb/domain/Person.java b/uniBlog/src/main/java/ro/ubb/domain/Person.java new file mode 100644 index 0000000..a6c3735 --- /dev/null +++ b/uniBlog/src/main/java/ro/ubb/domain/Person.java @@ -0,0 +1,75 @@ +package ro.ubb.domain; + +import java.io.Serializable; +import java.util.List; + +/** + * Created by tudorstanila on 14/12/2017. + */ +public abstract class Person implements Serializable { + + private String id; + private String firstName; + private String lastName; + private boolean isTeacher; + private List courses; + + public Person(String id, String firstName, String lastName, boolean isTeacher, List courses) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + this.isTeacher = isTeacher; + this.courses = courses; + } + + public boolean isTeacher() { + return isTeacher; + } + + public void setTeacher(boolean teacher) { + isTeacher = teacher; + } + + @Override + public String toString() { + return "Person{" + + "id='" + id + '\'' + + ", firstName='" + firstName + '\'' + + ", lastName='" + lastName + '\'' + + ", isTeacher=" + isTeacher + + ", courses=" + courses + + '}'; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public List getCourses() { + return courses; + } + + public void setCourses(List courses) { + this.courses = courses; + } +} diff --git a/uniBlog/src/main/java/ro/ubb/domain/Student.java b/uniBlog/src/main/java/ro/ubb/domain/Student.java new file mode 100644 index 0000000..41f319c --- /dev/null +++ b/uniBlog/src/main/java/ro/ubb/domain/Student.java @@ -0,0 +1,12 @@ +package ro.ubb.domain; + +import java.util.List; + +/** + * Created by tudorstanila on 14/12/2017. + */ +public class Student extends Person { + public Student(String id, String firstName, String lastName, boolean isTeacher, List courses) { + super(id, firstName, lastName, isTeacher, courses); + } +} diff --git a/uniBlog/src/main/java/ro/ubb/domain/Teacher.java b/uniBlog/src/main/java/ro/ubb/domain/Teacher.java new file mode 100644 index 0000000..29708b7 --- /dev/null +++ b/uniBlog/src/main/java/ro/ubb/domain/Teacher.java @@ -0,0 +1,12 @@ +package ro.ubb.domain; + +import java.util.List; + +/** + * Created by tudorstanila on 14/12/2017. + */ +public class Teacher extends Person { + public Teacher(String id, String firstName, String lastName, boolean isTeacher, List courses) { + super(id, firstName, lastName, isTeacher, courses); + } +} diff --git a/uniBlog/src/main/java/ro/ubb/repository/NewsRepository.java b/uniBlog/src/main/java/ro/ubb/repository/NewsRepository.java new file mode 100644 index 0000000..afa336a --- /dev/null +++ b/uniBlog/src/main/java/ro/ubb/repository/NewsRepository.java @@ -0,0 +1,57 @@ +package ro.ubb.repository; + +import ro.ubb.domain.Course; +import ro.ubb.domain.News; +import ro.ubb.domain.Person; +import ro.ubb.domain.Teacher; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by tudorstanila on 14/12/2017. + */ +public class NewsRepository { + private List mockData; + + public NewsRepository() { + this.mockData = new ArrayList(); + mockData.add(new News(1,new Teacher("1","Ion","Pop",true,new ArrayList()),"First news guys")); + mockData.add(new News(2,new Teacher("2","Florica","Patura",true,new ArrayList()),"Second news guys")); + } + + public List getNews(){ + return mockData; + } + + + public News insert(int Id,Person author, String content){ + News news = new News(Id,author,content); + this.mockData.add(news); + System.out.println(news); + return news; + } + + public Boolean findById(int id){ + News news = mockData.get(id); + if(news==null) + return false; + return true; + } + + public void delete(int id)throws Exception{ + if(!findById(id)) + throw new Exception(); + this.mockData.remove(id); + } + + public News update(int id,Person author,String content) throws Exception{ + if(!findById(id)) + throw new Exception(); + News auxNews=mockData.get(id); + auxNews.setAuthor(author); + auxNews.setText(content); + return auxNews; + } + +} diff --git a/uniBlog/src/main/java/ro/ubb/repository/PersonRepository.java b/uniBlog/src/main/java/ro/ubb/repository/PersonRepository.java new file mode 100644 index 0000000..9c7434e --- /dev/null +++ b/uniBlog/src/main/java/ro/ubb/repository/PersonRepository.java @@ -0,0 +1,33 @@ +package ro.ubb.repository; + + +import ro.ubb.domain.Course; +import ro.ubb.domain.Person; +import ro.ubb.domain.Student; +import ro.ubb.domain.Teacher; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by tudorstanila on 14/12/2017. + */ +public class PersonRepository { + private List mockData; + + public PersonRepository() { + this.mockData = new ArrayList(); + List courses = new ArrayList<>(); + courses.add(new Course("1","Mate")); + mockData.add(new Teacher("1","Ion","Pop",true,courses)); + mockData.add(new Student("1","Ana","Pop",false,courses)); + mockData.add(new Student("2","Ioana","Popa",false,new ArrayList())); + } + + public Person findById(String id){ + + return mockData.stream().filter(x->x.getId().equals(id)).findFirst().get(); + } + + +} diff --git a/uniBlog/src/main/java/ro/ubb/service/NewsService.java b/uniBlog/src/main/java/ro/ubb/service/NewsService.java new file mode 100644 index 0000000..03d92a9 --- /dev/null +++ b/uniBlog/src/main/java/ro/ubb/service/NewsService.java @@ -0,0 +1,58 @@ +package ro.ubb.service; + + +import org.h2.server.Service; +import ro.ubb.domain.News; +import ro.ubb.domain.Person; +import ro.ubb.repository.NewsRepository; +import ro.ubb.repository.PersonRepository; + +import java.util.List; +import java.util.stream.Collectors; + +/** + * Created by andreiuta on 14/12/2017. + */ +@Service +public class NewsService { + + private NewsRepository newsRepo; + private PersonRepository personRepo; + + public NewsService(NewsRepository newsRepo,PersonRepository personRepo) { + this.newsRepo = newsRepo; + this.personRepo = personRepo; + } + + public News addNews(int Id,String authorId, String content) + { + Person author = personRepo.findById(authorId); + if(author.isTeacher()) { + News inserted = newsRepo.insert(Id,author, content); + return inserted; + } + else{ + return null; + } + } + + public void deleteNews(int id){ + try{ + newsRepo.delete(id); + } + catch(Exception e){ + System.out.println(e.getMessage()); + } + + } + + public News updateNews(int id,Person author,String content){ + try { + News n = newsRepo.update(id, author, content); + return n; + }catch(Exception e){ + System.out.println(e.getMessage()); + } + return null; + } +}