Commit bef7a9dc by Bogdan Andjelkovic

profile picture upload - solved

parent 71ac5d4c
......@@ -7,10 +7,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class AppController
......@@ -43,10 +41,10 @@ public class AppController
}
@PostMapping("/register-proccess")
public String postRegisterProccess(@ModelAttribute Korisnik korisnik)
public String postRegisterProccess(@ModelAttribute Korisnik korisnik, @RequestParam("image") MultipartFile multipartFile)
{
System.out.println(korisnik);
kRepo.insert( korisnik);
kRepo.insert( korisnik, multipartFile);
return("redirect:/login");
}
......
......@@ -18,15 +18,7 @@ public class Korisnik
String sifra;
String telefon;
int tipId;
public Korisnik(String ime, String prezime, String email, String sifra, String telefon, int tipId) {
this.ime = ime;
this.prezime = prezime;
this.email = email;
this.sifra = sifra;
this.telefon = telefon;
this.tipId = tipId;
}
String slika;
@Override
public String toString() {
......
package com.example.SkuciSe.repository;
import com.example.SkuciSe.model.korisnik.Korisnik;
import com.example.SkuciSe.util.FileUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.sql.*;
import java.util.Optional;
......@@ -23,9 +27,17 @@ public class KorisnikRepository
}
}
public void insert(Korisnik korisnik)
public void insert( Korisnik korisnik, MultipartFile multipartFile)
{
String sql = "insert into korisnik( ime, prezime, telefon, email, sifra, tipId) values('"+korisnik.getIme()+"','"+korisnik.getPrezime()+"','"+korisnik.getTelefon()+"','"+korisnik.getEmail()+"','"+new BCryptPasswordEncoder().encode(korisnik.getSifra())+"',1)";
String fileName = StringUtils.cleanPath(multipartFile.getOriginalFilename());
String uploadDir = "user-photos";
try {
FileUploadUtil.saveFile(uploadDir, fileName, multipartFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
String sql = "insert into korisnik( ime, prezime, telefon, email, sifra, tipId, slika) values('"+korisnik.getIme()+"','"+korisnik.getPrezime()+"','"+korisnik.getTelefon()+"','"+korisnik.getEmail()+"','"+new BCryptPasswordEncoder().encode(korisnik.getSifra())+"',1, '"+fileName+"')";
System.out.println( sql);
try {
statement.executeUpdate( sql);
} catch (SQLException e) {
......@@ -41,7 +53,7 @@ public class KorisnikRepository
rs = statement.executeQuery( sql);
while( rs.next())
{
return ( new Korisnik( rs.getInt("korisnikId"), rs.getString("ime"), rs.getString("prezime"), rs.getString("email"), rs.getString("sifra"), rs.getString("telefon"), rs.getInt("tipid")));
return ( new Korisnik( rs.getInt("korisnikId"), rs.getString("ime"), rs.getString("prezime"), rs.getString("email"), rs.getString("sifra"), rs.getString("telefon"), rs.getInt("tipid"), rs.getString("slika")));
}
} catch (SQLException e) {
throw new RuntimeException(e);
......@@ -55,7 +67,7 @@ public class KorisnikRepository
rs = statement.executeQuery( sql);
while( rs.next())
{
return ( new Korisnik( rs.getInt("korisnikId"), rs.getString("ime"), rs.getString("prezime"), rs.getString("email"), rs.getString("sifra"), rs.getString("telefon"), rs.getInt("tipid")));
return ( new Korisnik( rs.getInt("korisnikId"), rs.getString("ime"), rs.getString("prezime"), rs.getString("email"), rs.getString("sifra"), rs.getString("telefon"), rs.getInt("tipid"), rs.getString("slika")));
}
} catch (SQLException e) {
throw new RuntimeException(e);
......
......@@ -80,7 +80,7 @@
</div>
<div class="col-md-12 mt-2" style="color:white;">
<input type="file" name="slika" accept="image/png, image/jpeg" />
<input type="file" name="image" accept="image/png, image/jpeg" />
<div class="invalid-feedback">Unesite sliku!</div>
</div>
......
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