// Retrieves plain event details in list format given event id public List <DefaultEventSearchDto> GetPlainEventDetailListById(int eId) { var e = new List <DefaultEventSearchDto>(); try { using (var ctx = new GreetNGroupContext()) { e = ctx.Events.Where(c => c.EventId.Equals(eId)) .Select(c => new DefaultEventSearchDto() { Uid = c.UserId, EventName = c.EventName, EventLocation = c.EventLocation, StartDate = c.StartDate }).ToList(); return(e); } } catch (ObjectDisposedException od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(e); } }
public HttpResponseMessage Logout(string email) { using (var ctx = new GreetNGroupContext()) { var userToLogout = ctx.Users.Where(u => u.UserName == email).FirstOrDefault <User>(); var JWTTokenToInvalidate = ctx.JWTTokens.Where(j => j.UserId == userToLogout.UserId) .OrderByDescending(p => p.Id).First(); if (JWTTokenToInvalidate != null) { var isTokenInvalidated = _jwtService.InvalidateToken(JWTTokenToInvalidate.Token); if (isTokenInvalidated) { var httpResponseSuccess = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("User has logged out of GreetNGroup") }; return(httpResponseSuccess); } var httpResponseFail = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent("Unable to delete token") }; return(httpResponseFail); } var httpResponse = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent("User Does Not Exist") }; return(httpResponse); } }
/* * The functions within this service make use of the database context * and similarly attempt to catch * ObjectDisposedException * to ensure the context is still valid and we want to catch the error * where it has been made * */ /// <summary> /// The following region handles inserts into the user table of the database /// </summary> #region Insert User Information // Inserts given user object into database public bool InsertUser(User user) { try { using (var ctx = new GreetNGroupContext()) { // Catch existing users if (user.UserName != null) { if (ctx.Users.Any(c => c.UserName.Equals(user.UserName))) { return(false); } } // Adds user ctx.Users.Add(user); ctx.SaveChanges(); return(true); } } catch (ObjectDisposedException od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(false); } }
// Retrieves plain event details in list format give partial event name/search input public List <DefaultEventSearchDto> GetPlainEventDetailListByName(string searchInput) { var e = new List <DefaultEventSearchDto>(); try { using (var ctx = new GreetNGroupContext()) { // Grabs events by needed columns and returns data transfer object e = ctx.Events.Where(uEvent => uEvent.EventName.Contains(searchInput)) .Select(n => new DefaultEventSearchDto() { Uid = n.UserId, EventId = n.EventId, EventName = n.EventName, EventLocation = n.EventLocation, StartDate = n.StartDate }).ToList(); return(e); } } catch (ObjectDisposedException od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(e); } }
/// <summary> /// The following region handles deletion of data from the user table in the database /// </summary> #region Delete User Information /* * For our application, the DeleteUser function is made to set user values to null * apart from UserId and userName which is used as reference */ public bool DeleteUser(User userToDelete) { try { using (var ctx = new GreetNGroupContext()) { /* * User(int uId, string firstName, string lastName, string userName, string city, * string state, string country, DateTime dob, bool isActivated) */ var user = ctx.Users.FirstOrDefault(c => c.UserId.Equals(userToDelete.UserId)); var blankUser = new User(user.UserId, null, null, "User has been deleted", null, null, null, DateTime.Now, false); if (user != null) { //ctx.Users.Remove(user); user = blankUser; ctx.SaveChanges(); return(true); } return(false); } } catch (ObjectDisposedException od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(false); } }
/// <summary> /// The following region handles Event information deletion /// </summary> #region Delete Event Information public bool DeleteEvent(int eId, int userId, string ip) { bool isSuccessfullyDeleted = false; try { using (var ctx = new GreetNGroupContext()) { Event curEvent = ctx.Events.FirstOrDefault(c => c.EventId.Equals(eId)); if (curEvent != null) { ctx.Events.Remove(curEvent); } ctx.SaveChanges(); isSuccessfullyDeleted = true; } LogGNGEventDeleted(userId.ToString(), eId, ip); return(isSuccessfullyDeleted); } catch (ObjectDisposedException od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(isSuccessfullyDeleted); } }
/// <summary> /// Checks to see if attendee aready exist in the table /// </summary> /// <param name="eventId">event id </param> /// <param name="userId">user id</param> /// <returns>a boolean if the attendee exist in the list or not</returns> public List <string> GetAttendees(int eventId) { var attendees = new List <Attendance>(); var user = new List <User>(); var names = new List <string>(); var ctx = new GreetNGroupContext(); try { attendees = ctx.Attendees.Where(c => c.EventId.Equals(eventId)).ToList(); if (attendees.Count != 0) { for (int i = 0; i < attendees.Count; i++) { user.Add(_userService.GetUserById(attendees[i].UserId)); } for (int i = 0; i < user.Count; i++) { names.Add(user[i].FirstName + " " + user[i].LastName); } return(names); } else { return(names); } } catch (ObjectDisposedException od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(names); } }
// Returns complete list of events sorted by those existing within a date range public List <Event> FindEventsByDateRange(string sDate, string eDate) { var resultList = new List <Event>(); var startDate = DateTime.Parse(sDate); var endDate = DateTime.Parse(eDate); // If the search start date exists after the end date if (startDate.CompareTo(endDate) > 0) { return(resultList); } try { using (var ctx = new GreetNGroupContext()) { // Return events where the startDate of the event is within the range of startDate and endDate // e stands for events resultList = ctx.Events.Where(e => e.StartDate.CompareTo(startDate) >= 0 && e.StartDate.CompareTo(endDate) <= 0).ToList(); // Sorts result by StartDate resultList.Sort((event1, event2) => DateTime.Compare(event1.StartDate, event2.StartDate)); // Removes events past current date resultList = FilterOutPastEvents(resultList); return(resultList); } } catch (Exception e) { _gngLoggerService.LogGNGInternalErrors(e.ToString()); throw; } }
public bool AddDefaultClaims(User newUser) { var isSuccessfulAdd = false; try { using (var ctx = new GreetNGroupContext()) { var userID = newUser.UserId; var claimToAdd = ctx.Claims.FirstOrDefault(c => c.ClaimId.Equals(1)); var claimToAdd2 = ctx.Claims.FirstOrDefault(c => c.ClaimId.Equals(2)); var claimToAdd3 = ctx.Claims.FirstOrDefault(c => c.ClaimId.Equals(8)); var claimToAdd4 = ctx.Claims.FirstOrDefault(c => c.ClaimId.Equals(9)); var userClaim = new UserClaim(userID, claimToAdd); var userClaim2 = new UserClaim(userID, claimToAdd2); var userClaim3 = new UserClaim(userID, claimToAdd3); var userClaim4 = new UserClaim(userID, claimToAdd4); ctx.UserClaims.Add(userClaim); ctx.UserClaims.Add(userClaim2); ctx.UserClaims.Add(userClaim3); ctx.UserClaims.Add(userClaim4); ctx.SaveChanges(); isSuccessfulAdd = true; return(isSuccessfulAdd); } } catch (Exception od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(isSuccessfulAdd); } }
// Removes pair of tagId and eventId where values match in database public bool DeleteEventTag(int eventId, string tag) { var isSuccessfulDelete = false; try { using (var ctx = new GreetNGroupContext()) { var eventTags = ctx.EventTags.Where(e => e.EventId.Equals(eventId)); var targetTagId = ctx.Tags.FirstOrDefault(t => t.TagName.Equals(tag)).TagId; foreach (var tags in eventTags) { if (tags.TagId.Equals(targetTagId)) { ctx.EventTags.Remove(tags); isSuccessfulDelete = true; } } ctx.SaveChanges(); } return(isSuccessfulDelete); } catch (ObjectDisposedException od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(isSuccessfulDelete); } }
public bool InvalidateToken(string jwtToken) { try { using (var ctx = new GreetNGroupContext()) { var userID = GetUserIDFromToken(jwtToken); var retrievedToken = ctx.JWTTokens.Where(j => j.Token == jwtToken) .Where(j => j.UserId == userID) .First(); if (retrievedToken != null) { retrievedToken.isValid = false; ctx.Entry(retrievedToken).State = EntityState.Modified; ctx.SaveChanges(); return(true); } return(false); } } catch { return(false); } }
public double GetRating(int userID) { GreetNGroupContext context = new GreetNGroupContext(); var listOfRatingsForUser = from r in context.UserRatings where r.RatedId1 == userID select r; var totalRating = 0.0; var totalRaters = 0.0; // Iterate thorugh list of ratings and get total rating and raters foreach (UserRating rating in listOfRatingsForUser) { totalRating = totalRating + rating.Rating; if (rating.Rating != 0) { totalRaters++; } } if (totalRaters == 0) { return(0); } var average = totalRating / totalRaters; average = Math.Round(average, 2); average = average * 100; return(average); }
/// <summary> /// Updates current user rating with a new rating /// </summary> /// <param name="rating">new Rating information</param> /// <param name="ip">user ip</param> /// <returns></returns> public bool UpdateRating(UserRating rating, string ip) { var userRating = new UserRating(); try { using (var context = new GreetNGroupContext()) { var query = context.UserRatings.Where(s => s.RaterId1 == rating.RaterId1 && s.RatedId1 == rating.RatedId1).FirstOrDefault <UserRating>(); if (query.Rating == rating.Rating) { query.Rating = 0; } else { query.Rating = rating.Rating; } context.SaveChanges(); } return(true); } catch (Exception e) { _gngLoggerService.LogGNGInternalErrors(e.ToString()); return(false); } }
public bool SetEventToExpired(int eId) { var isSuccessfulEventUpdate = false; try { if (IsEventExpired(eId) == true) { using (var ctx = new GreetNGroupContext()) { var eventToUpdate = ctx.Events.FirstOrDefault(e => e.EventId.Equals(eId)); eventToUpdate.IsEventExpired = true; ctx.SaveChanges(); } } else { return(isSuccessfulEventUpdate); } return(isSuccessfulEventUpdate); } catch (Exception e) { _gngLoggerService.LogGNGInternalErrors(e.ToString()); return(isSuccessfulEventUpdate); } }
public Attendance UpdateAttendance(Attendance updatedAtendance) { using (var _db = new GreetNGroupContext()) { _db.Entry(updatedAtendance).State = EntityState.Modified; _db.SaveChanges(); return(updatedAtendance); } }
/// <summary> /// Returns boolean if rating aleady exist in the databse /// </summary> /// <param name="raterID">Rater user ID</param> /// <param name="ratedID">the rated user ID</param> /// <returns>boolean if exist or not</returns> public bool GetRating(int raterID, int ratedID) { var userRating = false; using (var context = new GreetNGroupContext()) { userRating = context.UserRatings.Any(s => s.RaterId1 == raterID && s.RatedId1 == ratedID); } return(userRating); }
// Check attendees list of current event for current user public bool CheckAttendanceList(int eventId, int userId) { using (var ctx = new GreetNGroupContext()) { // Find if username exists within corresponding event attendee table var retrievedAttendee = ctx.Attendees.Where(a => a.EventId == eventId && a.UserId == userId); if (retrievedAttendee != null) { return(true); } return(false); } }
public HttpResponseMessage CheckIn(int eventID, int userID, string inputtedCheckInCode) { try { using (var ctx = new GreetNGroupContext()) { // perform search for current event and compares EventCheckInCode with input var retrievedEvent = ctx.Events.First(events => events.EventId.Equals(eventID)); var verified = retrievedEvent.EventCheckinCode.Equals(inputtedCheckInCode); if (!verified) { var httpResponseFail = new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent("Invalid Checkin Code") }; return(httpResponseFail); } var updatedAttendee = new Attendance(eventID, userID, true); var response = _attendeeService.UpdateAttendance(updatedAttendee); if (response == null) { var httpResponseFail = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent("Unable to check in") }; return(httpResponseFail); } var httpResponse = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("You have successfully checked in!") }; return(httpResponse); } } catch (Exception e) { //log var httpResponse = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(e.ToString()) }; return(httpResponse); } }
public HttpResponseMessage DeleteUser(string email) { try { using (var ctx = new GreetNGroupContext()) { var retrievedUser = ctx.Users.Where(u => u.UserName == email).FirstOrDefault <User>(); if (retrievedUser == null) { var httpResponseFail = new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent("User not found in system") }; return(httpResponseFail); } ctx.JWTTokens.RemoveRange(ctx.JWTTokens.Where(j => j.UserId == retrievedUser.UserId)); ctx.UserRatings.RemoveRange(ctx.UserRatings.Where(u => u.RatedId1 == retrievedUser.UserId)); ctx.UserRatings.RemoveRange(ctx.UserRatings.Where(u => u.RaterId1 == retrievedUser.UserId)); var retrievedClaims = ctx.UserClaims.Where(c => c.UId == retrievedUser.UserId).FirstOrDefault <UserClaim>(); ctx.UserClaims.Remove(retrievedClaims); retrievedUser.FirstName = "Deleted"; retrievedUser.LastName = "User"; retrievedUser.UserName = "******"; retrievedUser.State = "CA"; retrievedUser.City = "LB"; retrievedUser.Country = "USA"; retrievedUser.DoB = DateTime.Now; retrievedUser.IsActivated = false; ctx.SaveChanges(); var httpResponseSuccess = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("User was deleted from GreetNGroup") }; return(httpResponseSuccess); } } catch (Exception ex) { _gngLoggerService.LogGNGInternalErrors(ex.ToString()); var httpResponse = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(ex.ToString()) }; return(httpResponse); } }
/// <summary> /// This region checks for Claim information in the database /// </summary> #region Claim Information Check // Checks if claim exists within the database given claimId public bool IsClaimInTable(int claimId) { try { using (var ctx = new GreetNGroupContext()) { return(ctx.Claims.Any(u => u.ClaimId.Equals(claimId))); } } catch (ObjectDisposedException od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(false); } }
/// <summary> /// Checks to see if attendee aready exist in the table /// </summary> /// <param name="eventId">event id </param> /// <param name="userId">user id</param> /// <returns>a boolean if the attendee exist in the list or not</returns> public bool DoesAttendeeExist(int eventId, int userId) { try { using (var ctx = new GreetNGroupContext()) { return(ctx.Attendees.Any(c => c.EventId.Equals(eventId) && c.UserId.Equals(userId))); } } catch (ObjectDisposedException od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(false); } }
/// <summary> /// The following region retrieves confirmation of information within the event database /// </summary> #region Event Information Check public bool IsEventIdFound(int eventId) { try { using (var ctx = new GreetNGroupContext()) { return(ctx.Events.Any(c => c.EventId.Equals(eventId))); } } catch (ObjectDisposedException od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(false); } }
/// <summary> /// The following region checks information of users within the database /// </summary> #region User Information Check // Finds username by string and returns true if found public bool IsUsernameFound(string username) { try { using (var ctx = new GreetNGroupContext()) { return(ctx.Users.Any(u => u.UserName.Equals(username))); } } catch (ObjectDisposedException od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(false); } }
public bool IsClaimOnUser(int uId, int claimId) { try { using (var ctx = new GreetNGroupContext()) { var userClaims = ctx.UserClaims.Where(u => u.UId.Equals(uId)); return(userClaims.Any(c => c.ClaimId.Equals(claimId))); } } catch (Exception od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(false); } }
// Finds the next available userId public int GetNextUserID() { try { using (var ctx = new GreetNGroupContext()) { var user = ctx.Users.OrderByDescending(p => p.UserId).FirstOrDefault(); return(user.UserId + 1); } } catch (ObjectDisposedException od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(1); } }
/// <summary> /// The following region retrieves user information from the database /// </summary> #region User Information Retrieval // Returns userId found through username public int GetUserUid(string username) { try { using (var ctx = new GreetNGroupContext()) { var user = ctx.Users.FirstOrDefault(u => u.UserName.Equals(username)); return(user.UserId); } } catch (ObjectDisposedException od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(-1); } }
// Returns full list of events that are not before the current date public List <Event> FindAllEvents() { try { using (var ctx = new GreetNGroupContext()) { var eventList = ctx.Events.ToList(); eventList = FilterOutPastEvents(eventList); return(eventList); } } catch (Exception e) // Generic exception catch with specifics logged internally { _gngLoggerService.LogGNGInternalErrors(e.ToString()); throw; } }
/// <summary> /// The following region handles updating user information within the database /// </summary> #region Update User Information // Updates user by replacing user object in database with new user object with updated fields public bool UpdateUser(User updatedUser) { try { using (var ctx = new GreetNGroupContext()) { ctx.Entry(updatedUser).State = EntityState.Modified; ctx.SaveChanges(); return(true); } } catch (ObjectDisposedException od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(false); } }
// Returns user based on userId found public User GetUserById(int userID) { try { using (var ctx = new GreetNGroupContext()) { var user = ctx.Users.FirstOrDefault(c => c.UserId.Equals(userID)); return(user); } } catch (ObjectDisposedException od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(null); } }
// Finds the username by uId returns true if found public bool IsUsernameFoundById(int uId) { try { using (var ctx = new GreetNGroupContext()) { // Finds any username matching the parameter var user = ctx.Users.Any(s => s.UserId == uId); return(user); } } catch (ObjectDisposedException od) { _gngLoggerService.LogGNGInternalErrors(od.ToString()); return(false); } }