Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
th-app-java
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Amparo Oliver
th-app-java
Commits
8b8cbf9d
Commit
8b8cbf9d
authored
Feb 07, 2023
by
Oscar Gonzalez
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
se modifica birthday Controller
parent
ebe45f4b
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
6 deletions
+41
-6
curriculumsearch/src/main/java/com/roshka/controller/BirthdayController.java
+41
-6
No files found.
curriculumsearch/src/main/java/com/roshka/controller/BirthdayController.java
View file @
8b8cbf9d
...
@@ -2,18 +2,23 @@ package com.roshka.controller;
...
@@ -2,18 +2,23 @@ package com.roshka.controller;
import
com.roshka.modelo.Birthday
;
import
com.roshka.modelo.Birthday
;
import
com.roshka.repositorio.BirthdayRepository
;
import
com.roshka.repositorio.BirthdayRepository
;
import
org.apache.commons.codec.digest.DigestUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.PageRequest
;
import
org.springframework.data.domain.PageRequest
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.domain.Sort
;
import
org.springframework.data.domain.Sort
;
import
org.springframework.format.annotation.DateTimeFormat
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.stereotype.Controller
;
import
org.springframework.ui.Model
;
import
org.springframework.ui.Model
;
import
org.springframework.validation.BindingResult
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
import
javax.validation.Valid
;
import
java.io.IOException
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.Date
;
@Controller
@Controller
...
@@ -31,7 +36,6 @@ public class BirthdayController {
...
@@ -31,7 +36,6 @@ public class BirthdayController {
@GetMapping
(
path
=
{
"/agregar"
,
"/modificar/{id}"
})
@GetMapping
(
path
=
{
"/agregar"
,
"/modificar/{id}"
})
public
String
addBirthdayView
(
Model
model
,
@PathVariable
(
required
=
false
)
Long
id
)
{
public
String
addBirthdayView
(
Model
model
,
@PathVariable
(
required
=
false
)
Long
id
)
{
if
(
id
==
null
)
model
.
addAttribute
(
"cumple"
,
new
Birthday
());
if
(
id
==
null
)
model
.
addAttribute
(
"cumple"
,
new
Birthday
());
else
model
.
addAttribute
(
"cumple"
,
birthdayRepository
.
getById
(
id
));
else
model
.
addAttribute
(
"cumple"
,
birthdayRepository
.
getById
(
id
));
return
"birthday-form"
;
return
"birthday-form"
;
...
@@ -56,11 +60,42 @@ public class BirthdayController {
...
@@ -56,11 +60,42 @@ public class BirthdayController {
}
}
@PostMapping
(
path
=
{
"/agregar"
,
"/modificar/{id}"
})
@PostMapping
(
path
=
{
"/agregar"
,
"/modificar/{id}"
})
public
String
addBirthday
(
@Valid
@ModelAttribute
Birthday
birthday
,
BindingResult
result
,
@PathVariable
(
required
=
false
)
Long
id
,
Model
model
)
{
public
String
addBirthday
(
@RequestPart
(
name
=
"file"
)
MultipartFile
file
,
if
(
result
.
hasErrors
()
||
(
id
==
null
&&
birthdayRepository
.
existsByNombreCompletoIgnoreCase
(
birthday
.
getNombreCompleto
()))){
@RequestPart
(
name
=
"nombreCompleto"
)
String
nombreCompleto
,
@RequestPart
(
name
=
"idSlack"
)
String
idSlack
,
@RequestPart
(
name
=
"fecha"
)
String
fecha
,
@PathVariable
(
required
=
false
)
Long
id
,
Model
model
)
{
if
((
id
==
null
&&
birthdayRepository
.
existsByNombreCompletoIgnoreCase
(
nombreCompleto
))){
model
.
addAttribute
(
"mismoNombre"
,
true
);
model
.
addAttribute
(
"mismoNombre"
,
true
);
return
"birthday-form"
;
return
"birthday-form"
;
}
}
Birthday
birthday
=
new
Birthday
();
if
(
id
!=
null
)
{
birthday
=
birthdayRepository
.
getById
(
id
);
}
birthday
.
setNombreCompleto
(
nombreCompleto
);
birthday
.
setIdSlack
(
idSlack
);
SimpleDateFormat
formatter
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
);
try
{
Date
date
=
formatter
.
parse
(
fecha
);
birthday
.
setFecha
(
date
);
}
catch
(
ParseException
e
)
{
throw
new
RuntimeException
(
e
);
}
if
(!
file
.
isEmpty
())
{
Path
directorioImagenes
=
Paths
.
get
(
"images/"
+
DigestUtils
.
md5Hex
(
file
.
getOriginalFilename
())
+
".jpg"
);
String
rutaAbsoluta
=
directorioImagenes
.
toFile
().
getAbsolutePath
();
try
{
byte
[]
bytesImg
=
file
.
getBytes
();
Path
rutaCompleta
=
Paths
.
get
(
rutaAbsoluta
);
Files
.
write
(
rutaCompleta
,
bytesImg
);
// si todo salio bien guardamos la foto en la base de datos
birthday
.
setFoto
(
"http://localhost:8080/images/"
+
DigestUtils
.
md5Hex
(
file
.
getOriginalFilename
())
+
".jpg"
);
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
if
(
id
!=
null
)
birthday
.
setId
(
id
);
if
(
id
!=
null
)
birthday
.
setId
(
id
);
birthdayRepository
.
save
(
birthday
);
birthdayRepository
.
save
(
birthday
);
return
"redirect:/cumples"
;
return
"redirect:/cumples"
;
...
...
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