public bool RequestService(int userId, ServiceRequestDTO serviceRequest) { using (var entities = new UCAInvestDBEntities()) { var userDbResponse = entities.users.SingleOrDefault(t => t.user_id == userId); if (userDbResponse != null) { if (userDbResponse.status == "service_requested") { return(false); } long timeStamp = (long)TimeFunctions.DateTimeToUnixTimestamp(DateTime.Now); var location = LocationServices.GenerateDBGeographyFromCoordinates(serviceRequest.coordinates); UserService serviceToAdd = new UserService() { client_user_id = userId, request_timestamp = timeStamp, location = location, description = serviceRequest.description, taken_timestamp = 0, completed_timestamp = 0 }; entities.user_services.Add(serviceToAdd); userDbResponse.status = "service_requested"; entities.SaveChanges(); return(true); } } return(false); }
public bool TakeService(int craneUserId, int serviceId) { using (var entities = new UCAInvestDBEntities()) { var craneUser = entities.users.SingleOrDefault(t => t.user_id == craneUserId); if (craneUser == null || craneUser.status == "unavailable_crane") { return(false); } craneUser.status = "unavailable_crane"; entities.SaveChanges(); var serviceResponse = entities.user_services.SingleOrDefault(t => t.service_id == serviceId); if (serviceResponse != null && serviceResponse.taken_timestamp == 0) { serviceResponse.taken_timestamp = (long?)TimeFunctions.DateTimeToUnixTimestamp(DateTime.Now); serviceResponse.crane_user_id = craneUserId; var serviceUser = serviceResponse.ServiceUser; if (serviceUser != null) { serviceUser.status = "waiting_crane_arrival"; } entities.SaveChanges(); return(true); } return(false); } }
public RegisterUserDTO UserForToken(string token) { RegisterUserDTO response = null; using (var entities = new UCAInvestDBEntities()) { var dbResponse = entities.user_tokens.SingleOrDefault(t => t.token == token); if (dbResponse != null) { var expireTime = TimeFunctions.UnixTimeStampToDateTime(Convert.ToInt64(dbResponse.expire_timestamp)); if (expireTime < DateTime.Now) // Token expired, has to delete { entities.user_tokens.Remove(dbResponse); entities.SaveChanges(); } else { var userLinked = dbResponse.users; var config = new MapperConfiguration(cfg => { cfg.CreateMap <User, RegisterUserDTO>(); }); IMapper iMapper = config.CreateMapper(); response = iMapper.Map <User, RegisterUserDTO>(userLinked); } } } return(response); }
public UserTokenDTO GiveMeUniqueAvailableToken(int userId) { var GuidString = CryptoServices.GenerateToken(); long expireTime = Convert.ToInt64(TimeFunctions.DateTimeToUnixTimestamp( DateTime.Now.AddMinutes(GlobalConfig.TokenExpiresInMinutes))); using (var entities = new UCAInvestDBEntities()) { var dbResponse = entities.user_tokens.FirstOrDefault(t => t.token == GuidString); if (dbResponse == null) { var tokenToAdd = new UserToken() { user_id = userId, expire_timestamp = expireTime, token = GuidString }; entities.user_tokens.Add(tokenToAdd); entities.SaveChanges(); var config = new MapperConfiguration(cfg => { cfg.CreateMap <UserToken, UserTokenDTO>(); }); IMapper iMapper = config.CreateMapper(); return(iMapper.Map <UserToken, UserTokenDTO>(tokenToAdd)); } } return(GiveMeUniqueAvailableToken(userId)); }
public void FinishService(int userId, int serviceId) { using (var entities = new UCAInvestDBEntities()) { var serviceDbResponse = entities.user_services.SingleOrDefault(t => (t.crane_user_id == userId || t.client_user_id == userId) && t.service_id == serviceId); serviceDbResponse.ServiceUser.status = "freeuser"; serviceDbResponse.CraneUser.status = "available_crane"; serviceDbResponse.completed_timestamp = (long)TimeFunctions.DateTimeToUnixTimestamp(DateTime.Now); entities.SaveChanges(); } }
public List <ServicePinDTO> GetNearUntakenServices(CoordinateDTO coordinates) { var response = new List <ServicePinDTO>(); var currentLocation = GenerateDBGeographyFromCoordinates(coordinates); using (var entities = new UCAInvestDBEntities()) { var Services = (from u in entities.user_services where u.taken_timestamp == 0 && u.completed_timestamp == 0 && u.location.Distance(currentLocation) <= CommonConfig.IncidenceRadiusInM orderby u.location.Distance(currentLocation) select u); foreach (var service in Services) { response.Add(new ServicePinDTO(service.ServiceUser.username, new CoordinateDTO((double)service.location.Latitude, (double)service.location.Longitude), service.ServiceUser.plate, service.description, (long)service.request_timestamp, 2, service.ServiceUser.user_id, service.service_id)); } } return(response); }
public UserTokenDTO LoginUser(string username, string password) { UserTokenDTO response = null; using (var entities = new UCAInvestDBEntities()) { var dbResponse = entities.users.SingleOrDefault(t => t.username == username); if (dbResponse != null) { string salt = dbResponse.salt; if (dbResponse.password == CryptoServices.SHA256Encrypt(password, salt)) { response = GiveMeUniqueAvailableToken(dbResponse.user_id); } } } return(response); }
public List <MapPinDTO> GetNearCranes(CoordinateDTO coordinates) { var response = new List <MapPinDTO>(); var currentLocation = GenerateDBGeographyFromCoordinates(coordinates); using (var entities = new UCAInvestDBEntities()) { var Cranes = (from u in entities.user_locations where u.users.is_provider == 1 && u.location.Distance(currentLocation) <= CommonConfig.IncidenceRadiusInM orderby u.location.Distance(currentLocation) select u); foreach (var crane in Cranes) { response.Add(new MapPinDTO(crane.users.username, new CoordinateDTO((double)crane.location.Latitude, (double)crane.location.Longitude), crane.users.plate, "", (long)crane.timestamp, (int)crane.users.is_provider, crane.user_id)); } } return(response); }
public PublicUserDTO PublicUserById(int id) { PublicUserDTO response = null; using (var entities = new UCAInvestDBEntities()) { var dbResponse = entities.users.SingleOrDefault(t => t.user_id == id); if (dbResponse != null) { var config = new MapperConfiguration(cfg => { cfg.CreateMap <User, PublicUserDTO>(); }); IMapper iMapper = config.CreateMapper(); response = iMapper.Map <User, PublicUserDTO>(dbResponse); } } return(response); }
public UserTokenDTO RegisterUser(RegisterUserDTO userToRegister) { User addedUser = null; using (var entities = new UCAInvestDBEntities()) { var dbResponse = entities.users.SingleOrDefault(t => t.mail == userToRegister.mail || t.username == userToRegister.username); if (dbResponse == null) { long creationTimeStamp = Convert.ToInt64(TimeFunctions.DateTimeToUnixTimestamp(DateTime.Now)); string salt = CryptoServices.GenerateGuid().Substring(0, 8); string password = CryptoServices.SHA256Encrypt(userToRegister.password, salt); var userToAdd = new User() { first_name = userToRegister.first_name, mail = userToRegister.mail, last_name = userToRegister.last_name, mobile_phone = userToRegister.mobile_phone, creation_timestamp = creationTimeStamp, password = password, salt = salt, username = userToRegister.username, status = "freeuser" }; addedUser = entities.users.Add(userToAdd); entities.SaveChanges(); } } if (addedUser != null) { return(GiveMeUniqueAvailableToken(addedUser.user_id)); } return(null); }
public CurrentServicePositionsDTO ServicePosition(int userId) { CurrentServicePositionsDTO response = null; using (var entities = new UCAInvestDBEntities()) { var serviceDbResponse = entities.user_services.SingleOrDefault(t => (t.crane_user_id == userId || t.client_user_id == userId) && t.completed_timestamp == 0); if (serviceDbResponse != null) { response = new CurrentServicePositionsDTO(); var cranePosition = new CoordinateDTO( (double)serviceDbResponse.CraneUser.user_locations.location.Latitude, (double)serviceDbResponse.CraneUser.user_locations.location.Longitude); if (userId == serviceDbResponse.crane_user_id) { response.OtherUser = new UsersServices().PublicUserById((int)serviceDbResponse.client_user_id); response.OtherUserPosition = new CoordinateDTO((double)serviceDbResponse.ServiceUser.user_locations.location.Latitude, (double)serviceDbResponse.ServiceUser.user_locations.location.Longitude); } else { response.OtherUser = new UsersServices().PublicUserById((int)serviceDbResponse.crane_user_id); response.OtherUserPosition = cranePosition; } response.RequestedPosition = new CoordinateDTO((double)serviceDbResponse.location.Latitude, (double)serviceDbResponse.location.Longitude); response.ServiceId = serviceDbResponse.service_id; response.TimeRemaining = new GoogleTimeService().timeLeft(cranePosition, response.RequestedPosition); } } return(response); }
public void UpdatePosition(int userId, CoordinateDTO coordinates) { using (var entities = new UCAInvestDBEntities()) { var dbResponse = entities.user_locations.SingleOrDefault(t => t.user_id == userId); var currentLocation = GenerateDBGeographyFromCoordinates(coordinates); var currentTimestamp = (long?)TimeFunctions.DateTimeToUnixTimestamp(DateTime.Now); if (dbResponse != null) { dbResponse.location = currentLocation; dbResponse.timestamp = currentTimestamp; } else { entities.user_locations.Add(new UserLocation() { location = currentLocation, timestamp = currentTimestamp, user_id = userId }); } entities.SaveChanges(); } }