Commit 41cb081f by Patryk Czarnik

Klasa Ogloszenie i dostęp

parent 4025637e
package com.example.demo.model;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
@Entity
public class Ogloszenie {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String tytul;
private BigDecimal cena;
private LocalDateTime dataWystawienia;
public Ogloszenie() {
}
public Ogloszenie(Long id, String tytul, BigDecimal cena, LocalDateTime dataWystawienia) {
this.id = id;
this.tytul = tytul;
this.cena = cena;
this.dataWystawienia = dataWystawienia;
}
public Ogloszenie(String tytul, BigDecimal cena) {
this(null, tytul, cena, LocalDateTime.now());
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTytul() {
return tytul;
}
public void setTytul(String tytul) {
this.tytul = tytul;
}
public BigDecimal getCena() {
return cena;
}
public void setCena(BigDecimal cena) {
this.cena = cena;
}
public LocalDateTime getDataWystawienia() {
return dataWystawienia;
}
public void setDataWystawienia(LocalDateTime dataWystawienia) {
this.dataWystawienia = dataWystawienia;
}
@Override
public String toString() {
return "Ogloszenie [id=" + id + ", tytul=" + tytul + ", cena=" + cena + ", dataWystawienia=" + dataWystawienia + "]";
}
}
package com.example.demo.repo;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.model.Ogloszenie;
public interface OgloszeniaRepository extends JpaRepository<Ogloszenie, Long> {
}
\ No newline at end of file
package com.example.demo.rest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import com.example.demo.model.Customer;
import com.example.demo.model.Ogloszenie;
import com.example.demo.repo.CustomerRepository;
import com.example.demo.repo.OgloszeniaRepository;
@RestController
@RequestMapping(path="/rest/ogloszenia", produces="application/json")
public class ROgloszenia {
@Autowired
private OgloszeniaRepository ogloszeniaRepository;
@GetMapping
public Iterable<Ogloszenie> readAll() {
return ogloszeniaRepository.findAll();
}
@GetMapping("/{id}")
public Ogloszenie readOne(@PathVariable("id") long id) {
return ogloszeniaRepository.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
}
}
spring.datasource.url=jdbc:h2:mem:test
spring.h2.console.enabled=true
spring.h2.console.path=/h2
...@@ -9,13 +9,19 @@ ...@@ -9,13 +9,19 @@
<h1>Zabawy w H2</h1> <h1>Zabawy w H2</h1>
<form id="init-form" th:action="@{/init}"> <form id="init-form" th:action="@{/init}">
<button>Zainicjuj bazę</button> <button>Wgraj dane</button>
</form>
<form id="h2-console" th:action="@{/h2}" target="_blank">
<button>Konsola H2</button>
</form> </form>
<h2>Zapytania REST-owe</h2> <h2>Zapytania REST-owe</h2>
<ul> <ul>
<li><a th:href="@{/rest/customers}">/rest/customers</a></li> <li><a th:href="@{/rest/customers}">/rest/customers</a></li>
<li><a th:href="@{/rest/customers/1}">/rest/customers/1</a></li> <li><a th:href="@{/rest/customers/1}">/rest/customers/1</a></li>
<li><a th:href="@{/rest/ogloszenia}">/rest/ogloszenia</a></li>
<li><a th:href="@{/rest/ogloszenia/1}">/rest/ogloszenia/1</a></li>
</ul> </ul>
</body> </body>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment