Commit 20c53dbf by Bogdan Andjelkovic

insert oglas i slike - back

parent 64bf0e2e
......@@ -37,9 +37,9 @@ public class OglasController
}
@PostMapping("/novi-oglas/save")
public String postSaveNoviOglas( @ModelAttribute("newOglas") Oglas newOglas, @RequestParam("images") MultipartFile[] files)
public String postSaveNoviOglas( @ModelAttribute("newOglas") Oglas newOglas, @RequestParam("images") MultipartFile[] files, @AuthenticationPrincipal KorisnikDetails loggedUser)
{
oRepo.insert( newOglas, files);
oRepo.insert( loggedUser.getKorisnik().getKorisnikId(), newOglas, files);
return("redirect:/profile");
}
}
......@@ -22,5 +22,4 @@ public class Oglas
boolean grejanje;
boolean lift;
String lokacija;
}
......@@ -5,14 +5,53 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Base64;
@Component
public class OglasRepository
{
@Autowired
DataBase dataBase;
public void insert(Oglas newOglas, MultipartFile[] files)
public void insert( int korisnikId, Oglas newOglas, MultipartFile[] files)
{
int oglasId;
String sql = "insert into oglas( korisnikID, naslov, opis, lokacijaId, cena, vrstaoglasaid, kvadratura, brojsoba, namesten, grejanje, lift, lokacija)" +
"values ( " + korisnikId +
", '" + newOglas.getNaslov() +
"','" + newOglas.getOpis() +
"'," + newOglas.getLokacijaId() +
"," + newOglas.getCena() +
"," + newOglas.getVrstaOglasaId() +
"," + newOglas.getKvadratura() +
"," + newOglas.getBrojSoba() +
"," + newOglas.isNamesten() +
"," + newOglas.isGrejanje() +
"," + newOglas.isLift() +
",'" + newOglas.getLokacija() + "'";
System.out.println( sql);
try {
oglasId = dataBase.statement.executeUpdate( sql, Statement.RETURN_GENERATED_KEYS);
System.out.println( oglasId);
for( MultipartFile file: files)
{
sql = "insert into Slike( oglasid, slika)";
String filename = Base64.getEncoder().encodeToString( file.getBytes());
sql += " values ("+oglasId+",'"+filename+"')";
dataBase.statement.executeUpdate( sql);
}
System.out.println("slike inserted");
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
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