From cd2c9485231eaee3ace35a16ca55aa33afbfbeaa Mon Sep 17 00:00:00 2001 From: paoloenricocirone Date: Tue, 23 Jul 2019 16:40:13 +0200 Subject: [PATCH] Issue #185: Implementazione delle FAQ --- src/main/java/it/cnr/si/domain/Faq.java | 95 ++++++++ .../it/cnr/si/repository/FaqRepository.java | 18 ++ .../java/it/cnr/si/web/rest/FaqResource.java | 147 ++++++++++++ .../config/liquibase/cnr/base-faq.csv | 1 + .../20190723085228_added_entity_Faq.xml | 56 +++++ .../resources/config/liquibase/cnr/master.xml | 2 + .../config/liquibase/cnr/new/faq.csv | 4 + .../webapp/app/layouts/navbar/navbar.html | 8 +- .../manualistica/manualistica.controller.js | 6 +- .../app/pages/manualistica/manualistica.html | 9 +- src/main/webapp/app/services/cnr/data.js | 5 + src/main/webapp/i18n/en/global.json | 1 + src/main/webapp/i18n/it/global.json | 1 + src/main/webapp/index.html | 6 + .../cnr/si/web/rest/FaqResourceIntTest.java | 227 ++++++++++++++++++ 15 files changed, 582 insertions(+), 4 deletions(-) create mode 100644 src/main/java/it/cnr/si/domain/Faq.java create mode 100644 src/main/java/it/cnr/si/repository/FaqRepository.java create mode 100644 src/main/java/it/cnr/si/web/rest/FaqResource.java create mode 100644 src/main/resources/config/liquibase/cnr/base-faq.csv create mode 100644 src/main/resources/config/liquibase/cnr/changelog/20190723085228_added_entity_Faq.xml create mode 100644 src/main/resources/config/liquibase/cnr/new/faq.csv create mode 100644 src/test/java/it/cnr/si/web/rest/FaqResourceIntTest.java diff --git a/src/main/java/it/cnr/si/domain/Faq.java b/src/main/java/it/cnr/si/domain/Faq.java new file mode 100644 index 000000000..4d14bd0b0 --- /dev/null +++ b/src/main/java/it/cnr/si/domain/Faq.java @@ -0,0 +1,95 @@ +package it.cnr.si.domain; + +import org.hibernate.annotations.Cache; +import org.hibernate.annotations.CacheConcurrencyStrategy; + +import javax.persistence.*; +import javax.validation.constraints.NotNull; +import java.io.Serializable; +import java.util.Objects; + +/** + * A Faq. + */ +@Entity +@Table(name = "faq") +@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) +public class Faq implements Serializable { + + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @NotNull + @Column(name = "testo", nullable = false) + private String testo; + + @NotNull + @Column(name = "is_readable", nullable = false) + private Boolean isReadable; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getTesto() { + return testo; + } + + public Faq testo(String testo) { + this.testo = testo; + return this; + } + + public void setTesto(String testo) { + this.testo = testo; + } + + public Boolean isIsReadable() { + return isReadable; + } + + public Faq isReadable(Boolean isReadable) { + this.isReadable = isReadable; + return this; + } + + public void setIsReadable(Boolean isReadable) { + this.isReadable = isReadable; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Faq faq = (Faq) o; + if(faq.id == null || id == null) { + return false; + } + return Objects.equals(id, faq.id); + } + + @Override + public int hashCode() { + return Objects.hashCode(id); + } + + @Override + public String toString() { + return "Faq{" + + "id=" + id + + ", testo='" + testo + "'" + + ", isReadable='" + isReadable + "'" + + '}'; + } +} diff --git a/src/main/java/it/cnr/si/repository/FaqRepository.java b/src/main/java/it/cnr/si/repository/FaqRepository.java new file mode 100644 index 000000000..16b6ee2c4 --- /dev/null +++ b/src/main/java/it/cnr/si/repository/FaqRepository.java @@ -0,0 +1,18 @@ +package it.cnr.si.repository; + +import it.cnr.si.domain.Faq; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +import java.util.List; + +/** + * Spring Data JPA repository for the Faq entity. + */ +@SuppressWarnings("unused") +public interface FaqRepository extends JpaRepository { + + + @Query("select faq from Faq faq where faq.isReadable = TRUE") + public List getReadableFaq(); +} diff --git a/src/main/java/it/cnr/si/web/rest/FaqResource.java b/src/main/java/it/cnr/si/web/rest/FaqResource.java new file mode 100644 index 000000000..a2c153806 --- /dev/null +++ b/src/main/java/it/cnr/si/web/rest/FaqResource.java @@ -0,0 +1,147 @@ +package it.cnr.si.web.rest; + +import com.codahale.metrics.annotation.Timed; +import it.cnr.si.domain.Faq; +import it.cnr.si.repository.FaqRepository; +import it.cnr.si.web.rest.util.HeaderUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import javax.inject.Inject; +import javax.validation.Valid; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.List; +import java.util.Optional; + +/** + * REST controller for managing Faq. + */ +@RestController +@RequestMapping("/api") +public class FaqResource { + + private final Logger log = LoggerFactory.getLogger(FaqResource.class); + + @Inject + private FaqRepository faqRepository; + + /** + * POST /faqs : Create a new faq. + * + * @param faq the faq to create + * @return the ResponseEntity with status 201 (Created) and with body the new faq, or with status 400 (Bad Request) if the faq has already an ID + * @throws URISyntaxException if the Location URI syntax is incorrect + */ + @RequestMapping(value = "/faqs", + method = RequestMethod.POST, + produces = MediaType.APPLICATION_JSON_VALUE) + @Timed + public ResponseEntity createFaq(@Valid @RequestBody Faq faq) throws URISyntaxException { + log.debug("REST request to save Faq : {}", faq); + if (faq.getId() != null) { + return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert("faq", "idexists", "A new faq cannot already have an ID")).body(null); + } + Faq result = faqRepository.save(faq); + return ResponseEntity.created(new URI("/api/faqs/" + result.getId())) + .headers(HeaderUtil.createEntityCreationAlert("faq", result.getId().toString())) + .body(result); + } + + /** + * PUT /faqs : Updates an existing faq. + * + * @param faq the faq to update + * @return the ResponseEntity with status 200 (OK) and with body the updated faq, + * or with status 400 (Bad Request) if the faq is not valid, + * or with status 500 (Internal Server Error) if the faq couldnt be updated + * @throws URISyntaxException if the Location URI syntax is incorrect + */ + @RequestMapping(value = "/faqs", + method = RequestMethod.PUT, + produces = MediaType.APPLICATION_JSON_VALUE) + @Timed + public ResponseEntity updateFaq(@Valid @RequestBody Faq faq) throws URISyntaxException { + log.debug("REST request to update Faq : {}", faq); + if (faq.getId() == null) { + return createFaq(faq); + } + Faq result = faqRepository.save(faq); + return ResponseEntity.ok() + .headers(HeaderUtil.createEntityUpdateAlert("faq", faq.getId().toString())) + .body(result); + } + + /** + * GET /faqs : get all the faqs. + * + * @return the ResponseEntity with status 200 (OK) and the list of faqs in body + */ + @RequestMapping(value = "/faqs", + method = RequestMethod.GET, + produces = MediaType.APPLICATION_JSON_VALUE) + @Timed + public List getAllFaqs() { + log.debug("REST request to get all Faqs"); + List faqs = faqRepository.findAll(); + return faqs; + } + + + + /** + * GET /activeFaqs : get all readable faqs. + * + * @return the ResponseEntity with status 200 (OK) and the list of faqs readable in body + */ + @RequestMapping(value = "/faqs/readable", + method = RequestMethod.GET, + produces = MediaType.APPLICATION_JSON_VALUE) + @Timed + public List getReadableFaqs() { + log.debug("REST request to get all Faqs"); + List faqs = faqRepository.getReadableFaq(); + return faqs; + } + + /** + * GET /faqs/:id : get the "id" faq. + * + * @param id the id of the faq to retrieve + * @return the ResponseEntity with status 200 (OK) and with body the faq, or with status 404 (Not Found) + */ + @RequestMapping(value = "/faqs/{id}", + method = RequestMethod.GET, + produces = MediaType.APPLICATION_JSON_VALUE) + @Timed + public ResponseEntity getFaq(@PathVariable Long id) { + log.debug("REST request to get Faq : {}", id); + Faq faq = faqRepository.findOne(id); + return Optional.ofNullable(faq) + .map(result -> new ResponseEntity<>( + result, + HttpStatus.OK)) + .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND)); + } + + /** + * DELETE /faqs/:id : delete the "id" faq. + * + * @param id the id of the faq to delete + * @return the ResponseEntity with status 200 (OK) + */ + @RequestMapping(value = "/faqs/{id}", + method = RequestMethod.DELETE, + produces = MediaType.APPLICATION_JSON_VALUE) + @Timed + public ResponseEntity deleteFaq(@PathVariable Long id) { + log.debug("REST request to delete Faq : {}", id); + faqRepository.delete(id); + return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert("faq", id.toString())).build(); + } + +} diff --git a/src/main/resources/config/liquibase/cnr/base-faq.csv b/src/main/resources/config/liquibase/cnr/base-faq.csv new file mode 100644 index 000000000..753263618 --- /dev/null +++ b/src/main/resources/config/liquibase/cnr/base-faq.csv @@ -0,0 +1 @@ +id;testo;is_readable diff --git a/src/main/resources/config/liquibase/cnr/changelog/20190723085228_added_entity_Faq.xml b/src/main/resources/config/liquibase/cnr/changelog/20190723085228_added_entity_Faq.xml new file mode 100644 index 000000000..6173d9a62 --- /dev/null +++ b/src/main/resources/config/liquibase/cnr/changelog/20190723085228_added_entity_Faq.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/config/liquibase/cnr/master.xml b/src/main/resources/config/liquibase/cnr/master.xml index 6216749e9..d47d584a2 100644 --- a/src/main/resources/config/liquibase/cnr/master.xml +++ b/src/main/resources/config/liquibase/cnr/master.xml @@ -34,5 +34,7 @@ relativeToChangelogFile="false"/> + \ No newline at end of file diff --git a/src/main/resources/config/liquibase/cnr/new/faq.csv b/src/main/resources/config/liquibase/cnr/new/faq.csv new file mode 100644 index 000000000..d7583c2b1 --- /dev/null +++ b/src/main/resources/config/liquibase/cnr/new/faq.csv @@ -0,0 +1,4 @@ +id;testo;is_readable +1;Prova FAQ 1;TRUE +2;Prova FAQ 2;TRUE +3;Prova FAQ non leggibile;FALSE diff --git a/src/main/webapp/app/layouts/navbar/navbar.html b/src/main/webapp/app/layouts/navbar/navbar.html index f71657740..92c58b025 100644 --- a/src/main/webapp/app/layouts/navbar/navbar.html +++ b/src/main/webapp/app/layouts/navbar/navbar.html @@ -124,7 +124,7 @@
  • - +
  • @@ -199,6 +199,12 @@ External Message +
  • + +   + Faq + +
  • diff --git a/src/main/webapp/app/pages/manualistica/manualistica.controller.js b/src/main/webapp/app/pages/manualistica/manualistica.controller.js index 11da9c2d0..a19d6ceed 100644 --- a/src/main/webapp/app/pages/manualistica/manualistica.controller.js +++ b/src/main/webapp/app/pages/manualistica/manualistica.controller.js @@ -9,10 +9,14 @@ function ManualisticaController($scope, $state, dataService, utils) { var vm = this; - + //Recupero i Manuali dataService.manuali.getElenco().then(function(response) { vm.manuali = response.data; }); + //Recupero le Faq readable + dataService.faq.getReadable().then(function(faqs) { + vm.faqs = faqs.data; + }); $scope.downloadManuale = function(manuale) { diff --git a/src/main/webapp/app/pages/manualistica/manualistica.html b/src/main/webapp/app/pages/manualistica/manualistica.html index 2d8324dd4..f46820775 100644 --- a/src/main/webapp/app/pages/manualistica/manualistica.html +++ b/src/main/webapp/app/pages/manualistica/manualistica.html @@ -1,9 +1,7 @@
    -

    Manualistica

    - +

    Faq

    +
      +
    • + {{faq.testo}} +
    • + +
    diff --git a/src/main/webapp/app/services/cnr/data.js b/src/main/webapp/app/services/cnr/data.js index 344235fb0..e48194322 100644 --- a/src/main/webapp/app/services/cnr/data.js +++ b/src/main/webapp/app/services/cnr/data.js @@ -273,6 +273,11 @@ return $http.get("api/manual/" + nome, { responseType: 'arraybuffer' }); } }, + faq: { + getReadable: function () { + return $http.get("api/faqs/readable"); + } + }, signMany: function (username, password, otp, ids) { return $http({ url: "api/tasks/signMany", diff --git a/src/main/webapp/i18n/en/global.json b/src/main/webapp/i18n/en/global.json index e32af3ce3..b73eb5930 100644 --- a/src/main/webapp/i18n/en/global.json +++ b/src/main/webapp/i18n/en/global.json @@ -17,6 +17,7 @@ "membership": "Membership", "avviso": "Avviso", "externalMessage": "External Message", + "faq": "Faq", "jhipster-needle-menu-add-entry": "JHipster will add additional entities here (do not translate!)" }, "account": { diff --git a/src/main/webapp/i18n/it/global.json b/src/main/webapp/i18n/it/global.json index 9a7d22d63..af0d7603b 100644 --- a/src/main/webapp/i18n/it/global.json +++ b/src/main/webapp/i18n/it/global.json @@ -17,6 +17,7 @@ "membership": "Membership", "avviso": "Avviso", "externalMessage": "External Message", + "faq": "Faq", "jhipster-needle-menu-add-entry": "JHipster will add additional entities here (do not translate!)" }, "account": { diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index 79b431014..034a2b3aa 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -187,6 +187,12 @@ + + + + + + diff --git a/src/test/java/it/cnr/si/web/rest/FaqResourceIntTest.java b/src/test/java/it/cnr/si/web/rest/FaqResourceIntTest.java new file mode 100644 index 000000000..a57bd30d1 --- /dev/null +++ b/src/test/java/it/cnr/si/web/rest/FaqResourceIntTest.java @@ -0,0 +1,227 @@ +package it.cnr.si.web.rest; + +import it.cnr.si.SprintApp; +import it.cnr.si.domain.Faq; +import it.cnr.si.flows.ng.TestUtil; +import it.cnr.si.repository.FaqRepository; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import static org.hamcrest.Matchers.hasItem; +import org.mockito.MockitoAnnotations; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.data.web.PageableHandlerMethodArgumentResolver; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.PostConstruct; +import javax.inject.Inject; +import javax.persistence.EntityManager; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +/** + * Test class for the FaqResource REST controller. + * + * @see FaqResource + */ +@RunWith(SpringRunner.class) +@SpringBootTest(classes = SprintApp.class) +public class FaqResourceIntTest { + private static final String DEFAULT_TESTO = "AAAAA"; + private static final String UPDATED_TESTO = "BBBBB"; + + private static final Boolean DEFAULT_IS_READABLE = false; + private static final Boolean UPDATED_IS_READABLE = true; + + @Inject + private FaqRepository faqRepository; + + @Inject + private MappingJackson2HttpMessageConverter jacksonMessageConverter; + + @Inject + private PageableHandlerMethodArgumentResolver pageableArgumentResolver; + + @Inject + private EntityManager em; + + private MockMvc restFaqMockMvc; + + private Faq faq; + + @PostConstruct + public void setup() { + MockitoAnnotations.initMocks(this); + FaqResource faqResource = new FaqResource(); + ReflectionTestUtils.setField(faqResource, "faqRepository", faqRepository); + this.restFaqMockMvc = MockMvcBuilders.standaloneSetup(faqResource) + .setCustomArgumentResolvers(pageableArgumentResolver) + .setMessageConverters(jacksonMessageConverter).build(); + } + + /** + * Create an entity for this test. + * + * This is a static method, as tests for other entities might also need it, + * if they test an entity which requires the current entity. + */ + public static Faq createEntity(EntityManager em) { + Faq faq = new Faq(); + faq = new Faq() + .testo(DEFAULT_TESTO) + .isReadable(DEFAULT_IS_READABLE); + return faq; + } + + @Before + public void initTest() { + faq = createEntity(em); + } + + @Test + @Transactional + public void createFaq() throws Exception { + int databaseSizeBeforeCreate = faqRepository.findAll().size(); + + // Create the Faq + + restFaqMockMvc.perform(post("/api/faqs") + .contentType(TestUtil.APPLICATION_JSON_UTF8) + .content(TestUtil.convertObjectToJsonBytes(faq))) + .andExpect(status().isCreated()); + + // Validate the Faq in the database + List faqs = faqRepository.findAll(); + assertThat(faqs).hasSize(databaseSizeBeforeCreate + 1); + Faq testFaq = faqs.get(faqs.size() - 1); + assertThat(testFaq.getTesto()).isEqualTo(DEFAULT_TESTO); + assertThat(testFaq.isIsReadable()).isEqualTo(DEFAULT_IS_READABLE); + } + + @Test + @Transactional + public void checkTestoIsRequired() throws Exception { + int databaseSizeBeforeTest = faqRepository.findAll().size(); + // set the field null + faq.setTesto(null); + + // Create the Faq, which fails. + + restFaqMockMvc.perform(post("/api/faqs") + .contentType(TestUtil.APPLICATION_JSON_UTF8) + .content(TestUtil.convertObjectToJsonBytes(faq))) + .andExpect(status().isBadRequest()); + + List faqs = faqRepository.findAll(); + assertThat(faqs).hasSize(databaseSizeBeforeTest); + } + + @Test + @Transactional + public void checkIsReadableIsRequired() throws Exception { + int databaseSizeBeforeTest = faqRepository.findAll().size(); + // set the field null + faq.setIsReadable(null); + + // Create the Faq, which fails. + + restFaqMockMvc.perform(post("/api/faqs") + .contentType(TestUtil.APPLICATION_JSON_UTF8) + .content(TestUtil.convertObjectToJsonBytes(faq))) + .andExpect(status().isBadRequest()); + + List faqs = faqRepository.findAll(); + assertThat(faqs).hasSize(databaseSizeBeforeTest); + } + + @Test + @Transactional + public void getAllFaqs() throws Exception { + // Initialize the database + faqRepository.saveAndFlush(faq); + + // Get all the faqs + restFaqMockMvc.perform(get("/api/faqs?sort=id,desc")) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) + .andExpect(jsonPath("$.[*].id").value(hasItem(faq.getId().intValue()))) + .andExpect(jsonPath("$.[*].testo").value(hasItem(DEFAULT_TESTO.toString()))) + .andExpect(jsonPath("$.[*].isReadable").value(hasItem(DEFAULT_IS_READABLE.booleanValue()))); + } + + @Test + @Transactional + public void getFaq() throws Exception { + // Initialize the database + faqRepository.saveAndFlush(faq); + + // Get the faq + restFaqMockMvc.perform(get("/api/faqs/{id}", faq.getId())) + .andExpect(status().isOk()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) + .andExpect(jsonPath("$.id").value(faq.getId().intValue())) + .andExpect(jsonPath("$.testo").value(DEFAULT_TESTO.toString())) + .andExpect(jsonPath("$.isReadable").value(DEFAULT_IS_READABLE.booleanValue())); + } + + @Test + @Transactional + public void getNonExistingFaq() throws Exception { + // Get the faq + restFaqMockMvc.perform(get("/api/faqs/{id}", Long.MAX_VALUE)) + .andExpect(status().isNotFound()); + } + + @Test + @Transactional + public void updateFaq() throws Exception { + // Initialize the database + faqRepository.saveAndFlush(faq); + int databaseSizeBeforeUpdate = faqRepository.findAll().size(); + + // Update the faq + Faq updatedFaq = faqRepository.findOne(faq.getId()); + updatedFaq + .testo(UPDATED_TESTO) + .isReadable(UPDATED_IS_READABLE); + + restFaqMockMvc.perform(put("/api/faqs") + .contentType(TestUtil.APPLICATION_JSON_UTF8) + .content(TestUtil.convertObjectToJsonBytes(updatedFaq))) + .andExpect(status().isOk()); + + // Validate the Faq in the database + List faqs = faqRepository.findAll(); + assertThat(faqs).hasSize(databaseSizeBeforeUpdate); + Faq testFaq = faqs.get(faqs.size() - 1); + assertThat(testFaq.getTesto()).isEqualTo(UPDATED_TESTO); + assertThat(testFaq.isIsReadable()).isEqualTo(UPDATED_IS_READABLE); + } + + @Test + @Transactional + public void deleteFaq() throws Exception { + // Initialize the database + faqRepository.saveAndFlush(faq); + int databaseSizeBeforeDelete = faqRepository.findAll().size(); + + // Get the faq + restFaqMockMvc.perform(delete("/api/faqs/{id}", faq.getId()) + .accept(TestUtil.APPLICATION_JSON_UTF8)) + .andExpect(status().isOk()); + + // Validate the database is empty + List faqs = faqRepository.findAll(); + assertThat(faqs).hasSize(databaseSizeBeforeDelete - 1); + } +}