public HttpResponseMessage GetAddress(string UserCaller) { HttpResponseMessage msg; VoznjaRepository repo = new VoznjaRepository(); try { List <Voznja> list = repo.GetVoznje(); Voznja v = list.LastOrDefault(x => x.UserCallerID == UserCaller && (x.Status == DrivingStatus.Successful || x.Status == DrivingStatus.Failed)); //za musteriju //Voznja voz = list.Find(x => x.DriverID == UserCaller && (x.Status == DrivingStatus.Accepted || x.Status == DrivingStatus.InProgress)); //za vozaca if (v != null) { msg = Request.CreateResponse(HttpStatusCode.OK, v); } //else if (voz != null) //{ // msg = Request.CreateResponse(HttpStatusCode.OK, voz); //} else { msg = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No request in database."); } } catch (Exception e) { msg = Request.CreateErrorResponse(HttpStatusCode.BadRequest, $"Error - {e.Message}"); } return(msg); }
public HttpResponseMessage GetVoznja(int id) { HttpResponseMessage msg; VoznjaRepository repo = new VoznjaRepository(); try { Voznja v = repo.GetOneVoznja(id); if (v == null) { msg = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Drive doesn't exist"); } else { msg = Request.CreateResponse(HttpStatusCode.OK, v); } } catch (Exception e) { msg = Request.CreateErrorResponse(HttpStatusCode.BadRequest, $"Error - {e.Message}"); } return(msg); }
public HttpResponseMessage GetVoznje() { HttpResponseMessage msg; VoznjaRepository repo = new VoznjaRepository(); try { List <Voznja> list = repo.GetVoznje(); if (list.Count == 0) { msg = Request.CreateErrorResponse(HttpStatusCode.BadRequest, "There's no available drive right now."); } else { msg = Request.CreateResponse(HttpStatusCode.OK, list); } } catch (Exception e) { msg = Request.CreateErrorResponse(HttpStatusCode.BadRequest, $"Error - {e.Message}"); } return(msg); }
public HttpResponseMessage PostMusterija([FromBody] JToken token) { HttpResponseMessage msg; VoznjaRepository repo = new VoznjaRepository(); var start = token.Value <int>("start"); var driver = token.Value <string>("user"); var type = token.Value <string>("type"); var admin = token.Value <string>("admin"); var adminSts = token.Value <int>("adminStatus"); var driverSts = token.Value <int>("driverStatus"); TypeOfCar typeC = GetTypeInEnum(type); try { using (var db = new SystemDBContext()) { Voznja v = new Voznja() { StartPointID = start, DriverID = driver, TypeOfCar = typeC, Id = repo.GetVoznje().Count + 1, Status = GetStatus(adminSts), TimeOfReservation = DateTime.Now, AdminID = admin }; Vozac voz = db.Vozaci.FirstOrDefault(x => x.Username == driver); voz.DriveStatus = GetStatus(driverSts); Admin a = db.Admini.FirstOrDefault(x => x.Username == admin); a.DriveStatus = GetStatus(adminSts); db.Voznje.Add(v); db.SaveChanges(); msg = Request.CreateResponse(HttpStatusCode.Created, v); msg.Headers.Location = new Uri(Request.RequestUri + v.Id.ToString()); return(msg); } } catch (Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } }
public HttpResponseMessage PostMusterija([FromBody] JToken token) { HttpResponseMessage msg; VoznjaRepository repo = new VoznjaRepository(); var start = token.Value <int>("start"); var user = token.Value <string>("user"); //var driver = token.Value<string>("driver"); var type = token.Value <string>("type"); TypeOfCar typeC = GetTypeInEnum(type); try { using (var db = new SystemDBContext()) { Voznja v = new Voznja() { //DriverID = driver, StartPointID = start, UserCallerID = user, TypeOfCar = typeC, Id = repo.GetVoznje().Count + 1, Status = DrivingStatus.Created, TimeOfReservation = DateTime.Now }; Musterija m = db.Musterije.FirstOrDefault(x => x.Username == user); //mora se i korisniku promeniti status kad inicira poziv m.DriveStatus = DrivingStatus.Created; m.Commented = false; db.Voznje.Add(v); db.SaveChanges(); msg = Request.CreateResponse(HttpStatusCode.Created, v); msg.Headers.Location = new Uri(Request.RequestUri + v.Id.ToString()); return(msg); } } catch (Exception e) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, e)); } }
public HttpResponseMessage GetKomentar(string sortType) { HttpResponseMessage msg; VoznjaRepository repo = new VoznjaRepository(); KomentarRepository lrepo = new KomentarRepository(); List <Voznja> voznje = repo.GetVoznje(); List <Komentar> komentari = new KomentarRepository().GetKomentari(); List <Voznja> temp = new List <Voznja>(); try { if (sortType == "Date") { voznje = voznje.OrderBy(x => x.TimeOfReservation).ToList(); } else if (sortType == "Grade") { //voznje.Sort((a, b) => (lrepo.GetOneKomentar(a.StartPointID.Value).Grade.CompareTo(lrepo.GetOneKomentar(b.StartPointID.Value).Grade))); komentari = komentari.OrderByDescending(x => x.Grade).ToList(); komentari.ForEach(x => { voznje.ForEach(c => { if (x.Id == c.CommentID) { temp.Add(c); } }); }); voznje = temp; } msg = Request.CreateResponse(HttpStatusCode.OK, voznje); } catch (Exception e) { msg = Request.CreateErrorResponse(HttpStatusCode.BadRequest, $"Error - {e.Message}"); } return(msg); }
private void DeleteEntityVozac(Vozac voz, Musterija m) { VoznjaRepository repo = new VoznjaRepository(); List <Voznja> list = repo.GetVoznje(); using (SystemDBContext db = new SystemDBContext()) { voz = db.Vozaci.FirstOrDefault(x => x.Username == m.Username); voz.Role = m.Role; foreach (Voznja voznja in list) //ako brisemo vozaca, moramo i njegove voznje iz sistema, zbog RF u EF { if (voznja.DriverID == voz.Username) { db.Voznje.Remove(db.Voznje.FirstOrDefault(x => x.Id == voznja.Id)); } } Musterija mm = new Musterija() { Username = voz.Username, Email = voz.Email, Gender = voz.Gender, Jmbg = voz.Jmbg, Lastname = voz.Lastname, Name = voz.Name, Password = voz.Password, PhoneNumber = voz.PhoneNumber, Role = voz.Role }; db.Musterije.Add(mm); db.Vozaci.Remove(voz); db.SaveChanges(); } }