public HttpResponseMessage RegisterAccount([FromBody] Customer customer) { if (!DataRepository._customerRepo.CheckIfCustomerExists(customer.Username) && !DataRepository._dispatcherRepo.CheckIfDispatcherExists(customer.Username) && !DataRepository._driverRepo.CheckIfDriverExists(customer.Username)) { if (Validate(customer)) { customer.Id = Guid.NewGuid(); customer.Role = Enums.Roles.Customer; customer.IsBanned = false; LoginDto logObj = new LoginDto(); logObj.AccessToken = ServiceSecurity.MakeToken($"{customer.Username}:{customer.Password}"); customer.Password = ServiceSecurity.EncryptData(customer.Password, "password"); logObj.User = customer; DataRepository._customerRepo.NewCustomer(customer); return(Request.CreateResponse(HttpStatusCode.Created, logObj)); } else { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } } else { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } }
public HttpResponseMessage AddDriver([FromBody] Driver driver) { if (!DataRepository._customerRepo.CheckIfCustomerExists(driver.Username) && !DataRepository._dispatcherRepo.CheckIfDispatcherExists(driver.Username) && !DataRepository._driverRepo.CheckIfDriverExists(driver.Username)) { if (Validate(driver)) { driver.Id = Guid.NewGuid(); driver.Role = Enums.Roles.Driver; driver.Password = ServiceSecurity.EncryptData(driver.Password, "password"); driver.Occupied = false; driver.IsBanned = false; driver.Location = new Location { Address = "garage", X = 0, Y = 0 }; DataRepository._driverRepo.NewDriver(driver); return(Request.CreateResponse(HttpStatusCode.Created, DataRepository._driverRepo.RetriveDriverById(driver.Id))); } else { return(Request.CreateResponse(HttpStatusCode.InternalServerError)); } } else { return(Request.CreateResponse(HttpStatusCode.InternalServerError)); } }
public HttpResponseMessage SignIn([FromBody] LoginClass login) { if (DataRepository._driverRepo.LogIn(login.Username, ServiceSecurity.EncryptData(login.Password, "password"))) { Driver driver = DataRepository._driverRepo.RetriveDriverByUserName(login.Username); if (!driver.IsBanned) { LoginDto logObj = new LoginDto(); logObj.User = driver; logObj.AccessToken = ServiceSecurity.MakeToken($"{login.Username}:{login.Password}"); List <Drive> allDrives = DataRepository._driveRepo.GetAllDrives().ToList(); logObj.User.Drives = allDrives.FindAll(x => (x.DrivedBy != null) && (x.DrivedBy.Id == logObj.User.Id)); return(Request.CreateResponse(HttpStatusCode.OK, logObj)); } else { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } } else if (DataRepository._dispatcherRepo.LogIn(login.Username, ServiceSecurity.EncryptData(login.Password, "password"))) { LoginDto logObj = new LoginDto(); logObj.User = DataRepository._dispatcherRepo.RetriveDispatcherByUserName(login.Username); logObj.AccessToken = ServiceSecurity.MakeToken($"{login.Username}:{login.Password}"); List <Drive> allDrives = DataRepository._driveRepo.GetAllDrives().ToList(); logObj.User.Drives = allDrives.FindAll(x => (x.ApprovedBy != null) && (x.ApprovedBy.Id == logObj.User.Id)); return(Request.CreateResponse(HttpStatusCode.OK, logObj)); } else if (DataRepository._customerRepo.LogIn(login.Username, ServiceSecurity.EncryptData(login.Password, "password"))) { Customer customer = DataRepository._customerRepo.RetriveCustomerByUserName(login.Username); if (!customer.IsBanned) { LoginDto logObj = new LoginDto(); logObj.User = customer; logObj.User.Drives = (List <Drive>)DataRepository._driveRepo.GetAllDrivesForCustomerId(logObj.User.Id); logObj.AccessToken = ServiceSecurity.MakeToken($"{login.Username}:{login.Password}"); return(Request.CreateResponse(HttpStatusCode.OK, logObj)); } else { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } } else { return(Request.CreateResponse(HttpStatusCode.NotFound)); } }
public HttpResponseMessage UpdateDispatcher([FromBody] Dispatcher dispatcher) { if (DataRepository._customerRepo.CheckIfCustomerExists(dispatcher.Username) || DataRepository._dispatcherRepo.CheckIfDispatcherExists(dispatcher.Username) || DataRepository._driverRepo.CheckIfDriverExists(dispatcher.Username)) { dispatcher.Role = Enums.Roles.Dispatcher; dispatcher.Password = ServiceSecurity.EncryptData(dispatcher.Password, "password"); DataRepository._dispatcherRepo.EditDispatcherProfile(dispatcher); return(Request.CreateResponse(HttpStatusCode.OK, DataRepository._dispatcherRepo.RetriveDispatcherById(dispatcher.Id))); } else { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } }