public bool Update(Photo entity) { using (_logger.BeginScope(nameof(Update))) { if (entity == null) { _logger.LogWarning("ArgumentNullException while updating photo in DB"); return(false); } using (var context = new PsqlContext()) { try { DbPhoto dbPhoto = entity.ToDbEntity(); context.Photos.Update(dbPhoto); context.SaveChanges(); _logger.LogDebug("Photo updated successfully"); return(true); } catch (Exception e) { _logger.LogWarning(e.ToString() + " while updating photo in DB"); return(false); } } } }
public int GetLastPhotoIdInSession(int sessionId) { using (_logger.BeginScope(nameof(GetLastPhotoIdInSession))) { if (sessionId <= 0) { _logger.LogWarning("Ivalid Session ID"); throw new ArgumentException("Session ID cannot be equal zero or less"); } using (var context = new PsqlContext()) { try { var photo = context.Photos.Find(sessionId); if (photo == null) { _logger.LogWarning("Photo is not found"); throw new Exception("Photo of session with that ID is not found"); } _logger.LogDebug("PhotoID is found"); return(photo.PhotoId); } catch { _logger.LogWarning("Error"); throw; } } } }
public Photo GetLastPhotoInSession(int sessionId) { using (_logger.BeginScope(nameof(GetLastPhotoIdInSession))) { if (sessionId <= 0) { _logger.LogWarning("Ivalid Session ID"); throw new ArgumentException("Session ID cannot be equal zero or less"); } using (var context = new PsqlContext()) { try { var photos = from p in context.Photos where p.SessionId == sessionId select p; var photo = photos.OrderByDescending(d => d.Timestamp).FirstOrDefault(); if (photo == null) { _logger.LogWarning("Photo is not found"); throw new Exception("Photo of session with that ID is not found"); } Photo result = photo.ToDomainEntity(); _logger.LogDebug("Photo is found"); return(result); } catch { _logger.LogWarning("Error"); throw; } } } }
public bool Update(RecognitionResults entity) { using (logger.BeginScope(nameof(this.Update))) { if (entity == null) { logger.LogWarning("ArgumentNullException while updating Recognition Result in DB"); return(false); } using (var context = new PsqlContext()) { try { DbResult dbResult = entity.ToDbEntity(); context.Results.Update(dbResult); context.SaveChanges(); logger.LogDebug("RecognitionResult updated successfully"); return(true); } catch (Exception e) { logger.LogWarning(e.ToString() + " while updating pulse in DB"); return(false); } } } }
public RecognitionResults GetRecognitionResultsByPhotoId(int photoId) { if (photoId <= 0) { logger.LogWarning("Ivalid Photo ID"); throw new ArgumentException("Photo ID cannot be equal zero or less"); } using (var context = new PsqlContext()) { try { var recResult = context.Results. Where(p => p.PhotoId == photoId).FirstOrDefault(); if (recResult == null) { logger.LogWarning("RecognitionResults is not found"); throw new Exception("RecognitionResults with that Photo ID is not found"); } RecognitionResults result = recResult.ToDomainEntity(); logger.LogDebug("RecognitionResults is found"); return(result); } catch { logger.LogWarning("Error"); throw; } } }
public bool Add(Session entity) { using (_logger.BeginScope(nameof(Add))) { if (entity == null) { _logger.LogWarning("Entity is null, Add failed"); return(false); } var dbEntity = entity.ToDbEntity(); using (var context = new PsqlContext()) { context.Sessions.Add(dbEntity); try { context.SaveChanges(); } catch (Exception e) { _logger.LogWarning($"Exception from {e.Source}:{e.Message}, Add failed"); return(false); } } entity.SessionID = dbEntity.SessionId; _logger.LogDebug("Add succeeded"); return(true); } }
public Pulse GetLastPulseInSession(int sessionId) { using (logger.BeginScope(nameof(this.GetLastPulseInSession))) { if (sessionId <= 0) { logger.LogWarning("Ivalid Session ID"); throw new ArgumentException("Session ID cannot be equal zero or less"); } using (var context = new PsqlContext()) { try { var pulses = from p in context.Pulse where p.SessionId == sessionId select p; var pulse = pulses.OrderByDescending(d => d).FirstOrDefault(); if (pulse == null) { logger.LogWarning("Pulse is not found"); throw new Exception("Pulse of session with that ID is not found"); } Pulse result = pulse.ToDomainEntity(); logger.LogDebug("Pulse is found"); return result; } catch { logger.LogWarning("Error"); throw; } } } }
public bool Update(Pulse entity) { using (logger.BeginScope(nameof(this.Update))) { if (entity == null) { logger.LogWarning("ArgumentNullException while updating pulse in DB"); return false; } using (var context = new PsqlContext()) { try { DbPulse dbPulse = entity.ToDbEntity(); context.Pulse.Update(dbPulse); context.SaveChanges(); logger.LogDebug("Pulse updated successfully"); return true; } catch (Exception e) { logger.LogWarning(e.ToString() + " while updating pulse in DB"); return false; } } } }
public Photo GetPhotoById(int photoId) { using (_logger.BeginScope(nameof(GetPhotoById))) { using (var context = new PsqlContext()) { var result = (from p in context.Photos where p.PhotoId == photoId select p).FirstOrDefault(); return(result.ToDomainEntity()); } } }
public Session GetSessionById(int sessionId) { using (_logger.BeginScope(nameof(GetSessionById))) { using (var context = new PsqlContext()) { var result = (from s in context.Sessions where s.SessionId == sessionId select s).FirstOrDefault(); return(result.ToDomainEntity()); } } }
public IEnumerable <int> GetAllExpired() { using (_logger.BeginScope(nameof(GetAllExpired))) { using (var context = new PsqlContext()) { var cutOff = DateTime.Now; var result = from s in context.Sessions where s.ExpiresAt < cutOff select s.SessionId; return(result.ToList()); } } }
public bool Update(Session entity) { using (_logger.BeginScope(nameof(Update))) { if (entity == null) { _logger.LogWarning("Entity is null, Update failed"); return(false); } var dbEntity = entity.ToDbEntity(); using (var context = new PsqlContext()) { var oldEntity = (from session in context.Sessions where session.SessionId == dbEntity.SessionId select session).FirstOrDefault(); if (oldEntity == null) { _logger.LogWarning("Entity to update was not found, Update failed"); return(false); } oldEntity.Start = dbEntity.Start; oldEntity.Status = dbEntity.Status; try { context.SaveChanges(); } catch (Exception e) { _logger.LogWarning($"Exception from {e.Source}:{e.Message}, Update failed"); return(false); } } _logger.LogDebug("Update succeeded"); return(true); } }
public Session GetLastSessionForUser(int userId) { using (_logger.BeginScope(nameof(GetLastSessionForUser))) { using (var context = new PsqlContext()) { var result = (from session in context.Sessions where session.UserId == userId select session).FirstOrDefault(); if (result == null) { _logger.LogWarning($"No sessions found for user id {userId}, null session returned"); return(null); } _logger.LogDebug($"Last session successfully found for user id {userId}"); return(result.ToDomainEntity()); } } }