Commit c799f62f by Bogdan Andjelkovic

dodat grad za korisnika

parent 605dc3c8
...@@ -3,12 +3,11 @@ package com.example.SkuciSe.controller; ...@@ -3,12 +3,11 @@ package com.example.SkuciSe.controller;
import com.example.SkuciSe.model.korisnik.Korisnik; import com.example.SkuciSe.model.korisnik.Korisnik;
import com.example.SkuciSe.model.korisnik.KorisnikDetails; import com.example.SkuciSe.model.korisnik.KorisnikDetails;
import com.example.SkuciSe.repository.KorisnikRepository; import com.example.SkuciSe.repository.KorisnikRepository;
import com.example.SkuciSe.repository.LokacijaRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
...@@ -20,6 +19,8 @@ public class AppController ...@@ -20,6 +19,8 @@ public class AppController
{ {
@Autowired @Autowired
KorisnikRepository kRepo; KorisnikRepository kRepo;
@Autowired
LokacijaRepository lRepo;
@GetMapping({"/","","/index"}) @GetMapping({"/","","/index"})
public String getIndex(Model model, @AuthenticationPrincipal KorisnikDetails korisnik) public String getIndex(Model model, @AuthenticationPrincipal KorisnikDetails korisnik)
...@@ -42,6 +43,7 @@ public class AppController ...@@ -42,6 +43,7 @@ public class AppController
if( loggedUser != null ) if( loggedUser != null )
return "redirect:/index"; return "redirect:/index";
model.addAttribute("newUser", new Korisnik()); model.addAttribute("newUser", new Korisnik());
model.addAttribute("lokacije", lRepo.findAll());
return("register"); return("register");
} }
...@@ -63,6 +65,7 @@ public class AppController ...@@ -63,6 +65,7 @@ public class AppController
model.addAttribute("loggedUser", loggedUser); model.addAttribute("loggedUser", loggedUser);
model.addAttribute("editUser", loggedUser.getKorisnik()); model.addAttribute("editUser", loggedUser.getKorisnik());
model.addAttribute("profileRole", kRepo.findRoleById( loggedUser.getKorisnik().getTipId())); model.addAttribute("profileRole", kRepo.findRoleById( loggedUser.getKorisnik().getTipId()));
model.addAttribute("grad", lRepo.findById( loggedUser.getKorisnik().getGradId()));
return("profile"); return("profile");
} }
......
package com.example.SkuciSe.controller; package com.example.SkuciSe.controller;
import com.example.SkuciSe.model.korisnik.Korisnik; import com.example.SkuciSe.model.lokacija.Lokacija;
import com.example.SkuciSe.model.korisnik.KorisnikDetails;
import com.example.SkuciSe.repository.KorisnikRepository; import com.example.SkuciSe.repository.KorisnikRepository;
import com.example.SkuciSe.repository.LokacijaRepository;
import org.springframework.beans.factory.annotation.Autowired; 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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController @RestController
public class AppRestController public class AppRestController
{ {
@Autowired
KorisnikRepository kRepo;
@Autowired
LokacijaRepository lRepo;
@GetMapping("/listLokacija")
public List<Lokacija> getRegister()
{
return(lRepo.findAll() );
}
} }
\ No newline at end of file
...@@ -19,9 +19,9 @@ public class Korisnik { ...@@ -19,9 +19,9 @@ public class Korisnik {
String email; String email;
String sifra; String sifra;
String telefon; String telefon;
int tipId;
private String slika; private String slika;
int tipId;
int gradId;
@Override @Override
public String toString() { public String toString() {
...@@ -33,9 +33,8 @@ public class Korisnik { ...@@ -33,9 +33,8 @@ public class Korisnik {
", sifra='" + sifra + '\'' + ", sifra='" + sifra + '\'' +
", telefon='" + telefon + '\'' + ", telefon='" + telefon + '\'' +
", tipId=" + tipId + ", tipId=" + tipId +
", slika=" + slika + ", gradId=" + gradId +
", slika='" + slika + '\'' +
'}'; '}';
} }
} }
\ No newline at end of file
...@@ -58,6 +58,7 @@ public class KorisnikDetails implements UserDetails { ...@@ -58,6 +58,7 @@ public class KorisnikDetails implements UserDetails {
this.korisnik.setPrezime( korisnik.getPrezime()); this.korisnik.setPrezime( korisnik.getPrezime());
this.korisnik.setEmail( korisnik.getEmail()); this.korisnik.setEmail( korisnik.getEmail());
this.korisnik.setTelefon( korisnik.getTelefon()); this.korisnik.setTelefon( korisnik.getTelefon());
this.korisnik.setGradId( korisnik.getGradId());
} }
public void setKorisnikSlika( String slika) public void setKorisnikSlika( String slika)
{ {
......
package com.example.SkuciSe.repository; package com.example.SkuciSe.repository;
import com.example.SkuciSe.model.korisnik.Korisnik; import com.example.SkuciSe.model.korisnik.Korisnik;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException; import java.io.IOException;
import java.sql.*; import java.sql.*;
import java.util.Base64; import java.util.Base64;
import java.util.Optional;
@Component @Component
public class KorisnikRepository public class KorisnikRepository
{ {
Connection connection = null; @Autowired
Statement statement = null; DataBase dataBase;
public KorisnikRepository()
{ @Autowired
try { LokacijaRepository lRepo;
connection = DriverManager.getConnection("jdbc:mariadb://localhost/skucise","root","");
statement = connection.createStatement();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public void insert( Korisnik korisnik, MultipartFile multipartFile) throws IOException public void insert( Korisnik korisnik, MultipartFile multipartFile) throws IOException
{ {
String slika = Base64.getEncoder().encodeToString(multipartFile.getBytes()); String slika = Base64.getEncoder().encodeToString(multipartFile.getBytes());
System.out.println( slika); System.out.println( slika);
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, '"+ slika +"')"; String sql = "insert into korisnik( ime, prezime, telefon, email, sifra, tipId, slika, gradid) values('"+korisnik.getIme()+"','"+korisnik.getPrezime()+"','"+korisnik.getTelefon()+"','"+korisnik.getEmail()+"','"+new BCryptPasswordEncoder().encode(korisnik.getSifra())+"',1, '"+ slika +"', "+korisnik.getGradId()+")";
System.out.println( sql); System.out.println( sql);
try { try {
statement.executeUpdate( sql); dataBase.statement.executeUpdate( sql);
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
...@@ -47,10 +39,11 @@ public class KorisnikRepository ...@@ -47,10 +39,11 @@ public class KorisnikRepository
"',email = '" + korisnik.getEmail() + "',email = '" + korisnik.getEmail() +
"',sifra = '" + new BCryptPasswordEncoder().encode(korisnik.getSifra()) + "',sifra = '" + new BCryptPasswordEncoder().encode(korisnik.getSifra()) +
"',telefon = '" + korisnik.getTelefon() + "',telefon = '" + korisnik.getTelefon() +
"',gradId = '" + korisnik.getGradId() +
"' where korisnikid = " + korisnik.getKorisnikId(); "' where korisnikid = " + korisnik.getKorisnikId();
try { try {
System.out.println( sql); System.out.println( sql);
statement.executeUpdate( sql); dataBase.statement.executeUpdate( sql);
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
...@@ -64,7 +57,7 @@ public class KorisnikRepository ...@@ -64,7 +57,7 @@ public class KorisnikRepository
slika = Base64.getEncoder().encodeToString(file.getBytes()); slika = Base64.getEncoder().encodeToString(file.getBytes());
sql = " update korisnik " + sql = " update korisnik " +
" set slika = '" + slika + "' where korisnikid = " + korisnik.getKorisnikId(); " set slika = '" + slika + "' where korisnikid = " + korisnik.getKorisnikId();
statement.executeUpdate( sql); dataBase.statement.executeUpdate( sql);
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} catch (IOException e) { } catch (IOException e) {
...@@ -77,10 +70,10 @@ public class KorisnikRepository ...@@ -77,10 +70,10 @@ public class KorisnikRepository
String sql = "select * from korisnik where email = '"+email+"'"; String sql = "select * from korisnik where email = '"+email+"'";
ResultSet rs = null; ResultSet rs = null;
try { try {
rs = statement.executeQuery( sql); rs = dataBase.statement.executeQuery( sql);
while( rs.next()) 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"), rs.getString("slika"))); return ( new Korisnik( rs.getInt("korisnikId"), rs.getString("ime"), rs.getString("prezime"), rs.getString("email"), rs.getString("sifra"), rs.getString("telefon"), rs.getString("slika"), rs.getInt("tipid"), rs.getInt("gradid")));
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -91,10 +84,10 @@ public class KorisnikRepository ...@@ -91,10 +84,10 @@ public class KorisnikRepository
String sql = "select * from korisnik where KorisnikId = "+id; String sql = "select * from korisnik where KorisnikId = "+id;
ResultSet rs = null; ResultSet rs = null;
try { try {
rs = statement.executeQuery( sql); rs = dataBase.statement.executeQuery( sql);
while( rs.next()) 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"), rs.getString("slika"))); return ( new Korisnik( rs.getInt("korisnikId"), rs.getString("ime"), rs.getString("prezime"), rs.getString("email"), rs.getString("sifra"), rs.getString("telefon"), rs.getString("slika"), rs.getInt("tipid"), rs.getInt("gradid")));
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -107,7 +100,7 @@ public class KorisnikRepository ...@@ -107,7 +100,7 @@ public class KorisnikRepository
String sql = "select * from tipkorisnika where tipId = "+tipid; String sql = "select * from tipkorisnika where tipId = "+tipid;
ResultSet rs = null; ResultSet rs = null;
try { try {
rs = statement.executeQuery( sql); rs = dataBase.statement.executeQuery( sql);
while( rs.next()) while( rs.next())
{ {
return rs.getString("naziv"); return rs.getString("naziv");
......
package com.example.SkuciSe.repository; package com.example.SkuciSe.repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
@Component @Component
public class OglasRepository public class OglasRepository
{ {
Connection connection = null; @Autowired
Statement statement = null; DataBase dataBase;
public OglasRepository()
{
try {
connection = DriverManager.getConnection("jdbc:mariadb://localhost/skucise","root","");
statement = connection.createStatement();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
} }
...@@ -100,7 +100,7 @@ ...@@ -100,7 +100,7 @@
</div> </div>
<div class="d-flex align-items-center justify-content-between"> <div class="d-flex align-items-center justify-content-between">
<p class="py-2">Grad</p> <p class="py-2">Grad</p>
<p class="py-2 text-muted"> Kragujevac</p> <p class="py-2 text-muted" th:object="${grad}" th:text="${grad.naziv}"></p>
</div> </div>
</div> </div>
</div> </div>
......
...@@ -71,7 +71,10 @@ ...@@ -71,7 +71,10 @@
<div class="invalid-feedback">Email ne sme biti prazan!</div> <div class="invalid-feedback">Email ne sme biti prazan!</div>
</div> </div>
<select th:field="${newUser.gradId}">
<option th:each="lokacija: ${lokacije}" th:value="${lokacija.getLokacijaId()}" th:text="${lokacija.getNaziv()}">
</option>
</select>
<div class="col-md-12"> <div class="col-md-12">
<input th:field="${newUser.sifra}" class="form-control" type="password" name="password" placeholder="Sifra" required> <input th:field="${newUser.sifra}" class="form-control" type="password" name="password" placeholder="Sifra" required>
......
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