Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
P
pruebacrud
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Joel Cando
pruebacrud
Commits
b5773e3c
Commit
b5773e3c
authored
Sep 19, 2023
by
Joel Cando
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
este es el codigo cambiado
parent
325fdb36
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
87 additions
and
247 deletions
+87
-247
build.gradle
build.gradle
+1
-0
ProductoController.java
src/main/java/controller/ProductoController.java
+0
-82
StudentController.java
src/main/java/controller/StudentController.java
+33
-0
Mensaje.java
src/main/java/dto/Mensaje.java
+0
-18
ProductoDto.java
src/main/java/dto/ProductoDto.java
+0
-36
Producto.java
src/main/java/entty/Producto.java
+0
-47
Student.java
src/main/java/entty/Student.java
+17
-0
StudentRepository.java
src/main/java/repository/StudentRepository.java
+2
-7
ProductoService.java
src/main/java/service/ProductoService.java
+0
-45
StudentService.java
src/main/java/service/StudentService.java
+31
-0
application.properties
src/main/resources/application.properties
+3
-12
No files found.
build.gradle
View file @
b5773e3c
...
@@ -21,6 +21,7 @@ dependencies {
...
@@ -21,6 +21,7 @@ dependencies {
implementation
group:
'org.apache.commons'
,
name:
'commons-lang3'
,
version:
'3.13.0'
implementation
group:
'org.apache.commons'
,
name:
'commons-lang3'
,
version:
'3.13.0'
implementation
group:
'org.hibernate.validator'
,
name:
'hibernate-validator'
,
version:
'8.0.1.Final'
implementation
group:
'org.hibernate.validator'
,
name:
'hibernate-validator'
,
version:
'8.0.1.Final'
implementation
group:
'org.springframework.boot'
,
name:
'spring-boot-starter-parent'
,
version:
'3.1.3'
,
ext:
'pom'
implementation
group:
'org.springframework.boot'
,
name:
'spring-boot-starter-parent'
,
version:
'3.1.3'
,
ext:
'pom'
compileOnly
group:
'org.projectlombok'
,
name:
'lombok'
,
version:
'1.18.22'
runtimeOnly
'com.mysql:mysql-connector-j'
runtimeOnly
'com.mysql:mysql-connector-j'
testImplementation
'org.springframework.boot:spring-boot-starter-test'
testImplementation
'org.springframework.boot:spring-boot-starter-test'
...
...
src/main/java/controller/ProductoController.java
deleted
100644 → 0
View file @
325fdb36
package
controller
;
import
dto.Mensaje
;
import
dto.ProductoDto
;
import
entty.Producto
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatusCode
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
service.ProductoService
;
import
java.util.List
;
import
java.util.Optional
;
@RestController
@RequestMapping
(
"/producto"
)
@CrossOrigin
(
origins
=
"http://localhost:4200"
)
public
class
ProductoController
{
@Autowired
ProductoService
productoService
;
@GetMapping
(
"/lista"
)
public
ResponseEntity
<
List
<
Producto
>>
list
(){
List
<
Producto
>
list
=
productoService
.
list
();
return
new
ResponseEntity
(
list
,
HttpStatus
.
OK
);
}
@GetMapping
(
"/detail/{id}"
)
public
ResponseEntity
<
Producto
>
getById
(
@PathVariable
(
"id"
)
int
id
){
if
(!
productoService
.
existsById
(
id
))
return
new
ResponseEntity
(
new
Mensaje
(
"No existe"
),
HttpStatus
.
NOT_FOUND
);
Producto
producto
=
productoService
.
getOne
(
id
).
get
();
return
new
ResponseEntity
(
producto
,
HttpStatus
.
OK
);
}
@GetMapping
(
"/detail/{nombre}"
)
public
ResponseEntity
<
Producto
>
getByNombre
(
@PathVariable
(
"nombre"
)
String
nombre
){
if
(!
productoService
.
existsByNombre
(
nombre
))
return
new
ResponseEntity
(
new
Mensaje
(
"No existe"
),
HttpStatus
.
NOT_FOUND
);
Producto
producto
=
productoService
.
getByNombre
(
nombre
).
get
();
return
new
ResponseEntity
(
producto
,
HttpStatus
.
OK
);
}
@PostMapping
(
"/create"
)
public
ResponseEntity
<?>
create
(
@RequestBody
ProductoDto
productoDto
){
if
(
StringUtils
.
isBlank
(
productoDto
.
getNombre
()))
return
new
ResponseEntity
(
new
Mensaje
(
"El nombre es obligatorio"
),
HttpStatus
.
BAD_REQUEST
);
if
(
productoDto
.
getPrecio
()<
0
)
return
new
ResponseEntity
(
new
Mensaje
(
"El precio tiene que ser mayor 0"
),
HttpStatus
.
BAD_REQUEST
);
if
(
productoService
.
existsByNombre
(
productoDto
.
getNombre
()))
return
new
ResponseEntity
(
new
Mensaje
(
"el nombre ya existe"
),
HttpStatus
.
BAD_REQUEST
);
Producto
producto
=
new
Producto
(
productoDto
.
getNombre
(),
productoDto
.
getPrecio
());
productoService
.
save
(
producto
);
return
new
ResponseEntity
(
new
Mensaje
(
"producto creado"
),
HttpStatus
.
OK
);
}
@PostMapping
(
"/update/{id}"
)
public
ResponseEntity
<?>
update
(
@PathVariable
(
"id"
)
int
id
,
@RequestBody
ProductoDto
productoDto
){
if
(!
productoService
.
existsById
(
id
))
return
new
ResponseEntity
(
new
Mensaje
(
"No existe"
),
HttpStatus
.
NOT_FOUND
);
if
(
productoService
.
existsByNombre
(
productoDto
.
getNombre
())
&&
productoService
.
getByNombre
(
productoDto
.
getNombre
()).
get
().
getId
()
!=
id
)
return
new
ResponseEntity
(
new
Mensaje
(
"el nombre ya existe"
),
HttpStatus
.
BAD_REQUEST
);
if
(
StringUtils
.
isBlank
(
productoDto
.
getNombre
()))
return
new
ResponseEntity
(
new
Mensaje
(
"El nombre es obligatorio"
),
HttpStatus
.
BAD_REQUEST
);
if
(
productoDto
.
getPrecio
()<
0
)
return
new
ResponseEntity
(
new
Mensaje
(
"El precio tiene que ser mayor 0"
),
HttpStatus
.
BAD_REQUEST
);
Producto
producto
=
productoService
.
getOne
(
id
).
get
();
producto
.
setNombre
((
producto
.
getNombre
()));
producto
.
setPrecio
(
productoDto
.
getPrecio
());
productoService
.
save
(
producto
);
return
new
ResponseEntity
(
new
Mensaje
(
"producto creado"
),
HttpStatus
.
OK
);
}
@DeleteMapping
(
"/delete/{id}"
)
public
ResponseEntity
<?>
delete
(
@PathVariable
(
"id"
)
int
id
){
if
(!
productoService
.
existsById
(
id
))
return
new
ResponseEntity
(
new
Mensaje
(
"no existe"
),
HttpStatus
.
NOT_FOUND
);
productoService
.
delete
(
id
);
return
new
ResponseEntity
(
new
Mensaje
(
"producto eliminado"
),
HttpStatus
.
OK
);
}
}
src/main/java/controller/StudentController.java
0 → 100644
View file @
b5773e3c
package
controller
;
import
entty.Student
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.*
;
import
service.StudentService
;
import
java.util.List
;
import
java.util.Optional
;
@RestController
@RequestMapping
(
path
=
"api/v1/students"
)
public
class
StudentController
{
@Autowired
private
StudentService
studentService
;
@GetMapping
public
List
<
Student
>
getAll
(){
return
studentService
.
getStudents
();
}
@GetMapping
(
"/{studentId}"
)
public
Optional
<
Student
>
getById
(
@PathVariable
(
"studentId"
)
Long
studentId
){
return
studentService
.
getStudents
(
studentId
);
}
@PostMapping
public
void
saveUpdate
(
@RequestBody
Student
student
){
studentService
.
saveorUpdate
(
student
);
}
@DeleteMapping
(
"{studentId}"
)
public
void
saveUpdate
(
@PathVariable
(
"studentId"
)
long
studentId
){
studentService
.
delete
(
studentId
);
}
}
src/main/java/dto/Mensaje.java
deleted
100644 → 0
View file @
325fdb36
package
dto
;
public
class
Mensaje
{
private
String
mensaje
;
public
Mensaje
(
String
mensaje
)
{
this
.
mensaje
=
mensaje
;
}
public
String
getMensaje
()
{
return
mensaje
;
}
public
void
setMensaje
(
String
mensaje
)
{
this
.
mensaje
=
mensaje
;
}
}
src/main/java/dto/ProductoDto.java
deleted
100644 → 0
View file @
325fdb36
package
dto
;
import
jakarta.validation.constraints.Min
;
import
jakarta.validation.constraints.NotBlank
;
public
class
ProductoDto
{
@NotBlank
private
String
nombre
;
@Min
(
0
)
private
float
precio
;
public
ProductoDto
()
{
}
public
ProductoDto
(
@NotBlank
String
nombre
,
@Min
(
0
)
float
precio
)
{
this
.
nombre
=
nombre
;
this
.
precio
=
precio
;
}
public
String
getNombre
()
{
return
nombre
;
}
public
void
setNombre
(
String
nombre
)
{
this
.
nombre
=
nombre
;
}
public
float
getPrecio
()
{
return
precio
;
}
public
void
setPrecio
(
float
precio
)
{
this
.
precio
=
precio
;
}
}
src/main/java/entty/Producto.java
deleted
100644 → 0
View file @
325fdb36
package
entty
;
import
jakarta.persistence.Entity
;
import
jakarta.persistence.GeneratedValue
;
import
jakarta.persistence.GenerationType
;
import
jakarta.persistence.Id
;
@Entity
public
class
Producto
{
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
private
int
id
;
private
String
nombre
;
private
float
precio
;
public
Producto
()
{
}
public
Producto
(
String
nombre
,
float
precio
)
{
this
.
nombre
=
nombre
;
this
.
precio
=
precio
;
}
public
int
getId
()
{
return
id
;
}
public
void
setId
(
int
id
)
{
this
.
id
=
id
;
}
public
String
getNombre
()
{
return
nombre
;
}
public
void
setNombre
(
String
nombre
)
{
this
.
nombre
=
nombre
;
}
public
float
getPrecio
()
{
return
precio
;
}
public
void
setPrecio
(
float
precio
)
{
this
.
precio
=
precio
;
}
}
src/main/java/entty/Student.java
0 → 100644
View file @
b5773e3c
package
entty
;
import
jakarta.persistence.*
;
import
lombok.Data
;
@Data
@Entity
@Table
(
name
=
"tbl_student"
)
public
class
Student
{
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
IDENTITY
)
private
Long
studentId
;
private
String
fisrName
;
private
String
lastName
;
@Column
(
name
=
"email_address"
,
unique
=
true
,
nullable
=
false
)
private
String
email
;
}
src/main/java/repository/
Producto
Repository.java
→
src/main/java/repository/
Student
Repository.java
View file @
b5773e3c
package
repository
;
package
repository
;
import
entty.
Producto
;
import
entty.
Student
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
org.springframework.stereotype.Repository
;
import
org.springframework.stereotype.Repository
;
import
java.util.Optional
;
@Repository
@Repository
public
interface
ProductoRepository
extends
JpaRepository
<
Producto
,
Integer
>
{
public
interface
StudentRepository
extends
JpaRepository
<
Student
,
Long
>
{
Optional
<
Producto
>
findByNombre
(
String
nombre
);
boolean
existsByNombre
(
String
nombre
);
}
}
src/main/java/service/ProductoService.java
deleted
100644 → 0
View file @
325fdb36
package
service
;
import
entty.Producto
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
repository.ProductoRepository
;
import
java.util.List
;
import
java.util.Optional
;
@Service
@Transactional
public
class
ProductoService
{
@Autowired
ProductoRepository
productoRepository
;
public
List
<
Producto
>
list
()
{
return
productoRepository
.
findAll
();
}
public
Optional
<
Producto
>
getOne
(
int
id
)
{
return
productoRepository
.
findById
(
id
);
}
public
Optional
<
Producto
>
getByNombre
(
String
nombre
)
{
return
productoRepository
.
findByNombre
(
nombre
);
}
public
void
save
(
Producto
producto
)
{
productoRepository
.
save
(
producto
);
}
public
void
delete
(
int
id
)
{
productoRepository
.
deleteById
(
id
);
}
public
boolean
existsById
(
int
id
)
{
return
productoRepository
.
existsById
(
id
);
}
public
boolean
existsByNombre
(
String
nombre
)
{
return
productoRepository
.
existsByNombre
(
nombre
);
}
}
src/main/java/service/StudentService.java
0 → 100644
View file @
b5773e3c
package
service
;
import
entty.Student
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
repository.StudentRepository
;
import
java.util.List
;
import
java.util.Optional
;
@Service
public
class
StudentService
{
@Autowired
StudentRepository
studentRepository
;
public
List
<
Student
>
getStudents
(){
return
studentRepository
.
findAll
();
}
public
Optional
<
Student
>
getStudents
(
Long
id
){
return
studentRepository
.
findById
(
id
);
}
public
void
saveorUpdate
(
Student
student
){
studentRepository
.
save
(
student
);
}
public
void
delete
(
Long
id
){
studentRepository
.
deleteById
(
id
);
}
}
src/main/resources/application.properties
View file @
b5773e3c
...
@@ -2,14 +2,6 @@ spring.datasource.url=jdbc:mysql://localhost:3306/crud
...
@@ -2,14 +2,6 @@ spring.datasource.url=jdbc:mysql://localhost:3306/crud
spring.datasource.username
=
root
spring.datasource.username
=
root
spring.datasource.password
=
spring.datasource.password
=
spring.datasource.driver-class-name
=
com.mysql.jdbc.Driver
spring.jpa.show-sql
=
true
spring.jpa.show-sql
=
true
spring.jpa.hibernate.ddl-auto
=
create-drop
spring.jpa.hibernate.ddl-auto
=
none
#spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
server.port
=
9090
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment