Advertisement
MikecIT

App

Jan 18th, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.12 KB | None | 0 0
  1. package rest;
  2.  
  3. import static spark.Spark.after;
  4. import static spark.Spark.before;
  5. import static spark.Spark.get;
  6. import static spark.Spark.halt;
  7. import static spark.Spark.port;
  8. import static spark.Spark.post;
  9. import static spark.Spark.staticFiles;
  10.  
  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.util.ArrayList;
  14. import java.util.HashMap;
  15.  
  16. import com.google.gson.Gson;
  17. import com.google.gson.JsonObject;
  18. import com.google.gson.reflect.TypeToken;
  19.  
  20. import beans.Korisnik;
  21. import beans.Racun;
  22. import beans.Uplata;
  23. import spark.Session;
  24.  
  25. public class App {
  26.  
  27.     private static Gson g = new Gson();
  28.     private static HashMap<String, Racun> racuni = new HashMap<String, Racun>();
  29.     private static HashMap<String, Korisnik> korisnici = new HashMap<String, Korisnik>() {{
  30.         put("1", new Korisnik("1", "1"));
  31.         put("2", new Korisnik("2", "2"));
  32.     }};
  33.    
  34.     public static void main(String[] args) throws IOException {
  35.         port(8080);
  36.        
  37.         staticFiles.externalLocation(new File("./static").getCanonicalPath());
  38.        
  39.         post("/rest/login", (req, res) -> {
  40.             res.type("application/json");
  41.             Korisnik k = g.fromJson(req.body(), Korisnik.class);
  42.             Session ss = req.session(true);
  43.             Korisnik korisnikSession = ss.attribute("user");
  44.             String msg = "false";
  45.            
  46.             if(korisnikSession == null) {
  47.                 if(korisnici.containsKey(k.getUsername())) {
  48.                     if(korisnici.get(k.getUsername()).getPassword().equals(k.getPassword())) {
  49.                         ss.attribute("user", korisnici.get(k.getUsername()));
  50.                         msg = "true";
  51.                     }
  52.                 }
  53.             }
  54.            
  55.             return "{\"msg\": " + msg + "}";
  56.         });
  57.        
  58.         get("/rest/logout", (req, res) -> {
  59.             res.type("application/json");
  60.             Session ss = req.session(true);
  61.             Korisnik k = ss.attribute("user");
  62.            
  63.             if(k != null)
  64.             {
  65.                 ss.invalidate();
  66.             }
  67.                
  68.             return "{\"done\": true}";
  69.            
  70.         });
  71.        
  72.         after("/rest/logout", (req, res) -> {
  73.             Session ss = req.session(true);
  74.             Korisnik k = ss.attribute("user");
  75.            
  76.             if(k == null)
  77.                 res.redirect("/login.html", 301);
  78.         });
  79.        
  80.         get("/rest/checkLogin", (req, res) -> {
  81.             Session ss = req.session(true);
  82.             Korisnik k = ss.attribute("user");
  83.            
  84.             return k == null ? "{\"logged\": false}" : "{\"logged\": true}";
  85.         });
  86.        
  87.         before("/rest/addRacun", (req, res) -> {
  88.             Session ss = req.session(true);
  89.             Korisnik k = ss.attribute("user");
  90.            
  91.             if(k == null)
  92.                 halt(403, "<h1>403 Unauthorized function.</h1>");
  93.         });
  94.        
  95.         post("/rest/addRacun", (req, res) -> {
  96.             Session ss = req.session(true);
  97.             Korisnik k = ss.attribute("user");
  98.            
  99.             Racun r = null;
  100.             try {
  101.                 r = g.fromJson(req.body(), Racun.class);
  102.             } catch (Exception e) {
  103.                 return "{\"msg\": \"errorFields\"}";
  104.             }
  105.            
  106.             String msg = "false";
  107.            
  108.             if(!k.hasRacun(r.getBrojRacuna()) && !racuni.containsKey(r.getBrojRacuna())) {
  109.                 k.addRacun(r.getBrojRacuna());
  110.                 racuni.put(r.getBrojRacuna(), r);
  111.                 msg = "true";
  112.             }
  113.            
  114.             return "{\"msg\": " + msg + "}";
  115.         });
  116.        
  117.         before("/rest/getRacuni", (req, res) -> {
  118.             Session ss = req.session(true);
  119.             Korisnik k = ss.attribute("user");
  120.            
  121.             if(k == null)
  122.                 halt(403, "<h1>403 Unauthorized function.</h1>");
  123.         });
  124.        
  125.         get("/rest/getRacuni", (req, res) -> {
  126.             Session ss = req.session(true);
  127.             Korisnik k = ss.attribute("user");
  128.            
  129.             ArrayList<Racun> racuniKorisnika = new ArrayList<Racun>();
  130.            
  131.             for(Racun r : racuni.values()) {
  132.                 if(k.hasRacun(r.getBrojRacuna())) {
  133.                     racuniKorisnika.add(r);
  134.                 }
  135.             }
  136.            
  137.             return g.toJson(racuniKorisnika);
  138.         });
  139.        
  140.         before("/rest/uplati", (req, res) -> {
  141.             Session ss = req.session(true);
  142.             Korisnik k = ss.attribute("user");
  143.            
  144.             if(k == null)
  145.                 halt(403, "<h1>403 Unauthorized function.</h1>");
  146.         });
  147.        
  148.         post("/rest/uplati", (req, res) -> {
  149.             Session ss = req.session(true);
  150.             Korisnik k = ss.attribute("user");
  151.            
  152.  
  153.            
  154.                
  155.            
  156.             Uplata u = null;
  157.                
  158.             try {
  159.                  u = g.fromJson(req.body(), Uplata.class);
  160.                  if(u.getRacun() == null)
  161.                      throw new Exception();
  162.             } catch(Exception e) {
  163.                 res.status(404);
  164.                 return "Niste uneli sva polja!";
  165.             }
  166.            
  167.             System.out.println(u);
  168.            
  169.             if(racuni.containsKey(u.getRacun()))
  170.                 if(k.hasRacun(u.getRacun()))
  171.                     racuni.get(u.getRacun()).uplati(u.getIznos());
  172.             System.out.println(u.getRacun());
  173.             System.out.println(racuni.containsKey(u.getRacun()));
  174.             return g.toJson(racuni.get(u.getRacun()));
  175.         });
  176.        
  177.         get("/rest/removeRacun", (req, res) -> {
  178.             Session ss = req.session(true);
  179.             Korisnik k = ss.attribute("user");
  180.             String brRacuna = req.queryParams("brRacuna");
  181.             String msg = "false";
  182.            
  183.             if(k.hasRacun(brRacuna) && racuni.containsKey(brRacuna)) {
  184.                 k.removeRacun(brRacuna);
  185.                 racuni.remove(brRacuna);
  186.                 msg = "true";
  187.             }
  188.            
  189.             return "{\"msg\": " + msg + "}";
  190.         });
  191.        
  192.         get("/rest/toggleRacun", (req, res) -> {
  193.             Session ss = req.session(true);
  194.             Korisnik k = ss.attribute("user");
  195.             String brRacuna = req.queryParams("brRacuna");
  196.            
  197.             if(k.hasRacun(brRacuna) && racuni.containsKey(brRacuna)) {
  198.                 racuni.get(brRacuna).toggleAktivan();
  199.             }
  200.            
  201.             return g.toJson(racuni.get(brRacuna));
  202.         });
  203.     }
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement