public HttpResponseMessage DeleteUser(DeleteUserData deleteInfo) { var accessLevelGetter = GetAccessLevel(deleteInfo.AdminUserName).Content; int accessLevel = (int)((ObjectContent)accessLevelGetter).Value; if (accessLevel == 2) { try { using (NipsDBEntities entities = new NipsDBEntities()) { var lastName = deleteInfo.DeleteName.Split(',')[0]; var firstName = deleteInfo.DeleteName.Split(',')[1]; var id = entities.Users.FirstOrDefault(u => u.LastName == lastName && u.FirstName == firstName).ID; entities.AspNetUsers.Remove(entities.AspNetUsers.FirstOrDefault(u => u.Id == id)); entities.Ledgers.RemoveRange(entities.Ledgers.Where(l => l.GetterID == id)); entities.Users.Remove(entities.Users.FirstOrDefault(u => u.ID == id)); entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.Accepted)); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } } else { return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } }
public HttpResponseMessage Delete(DeleteLedgerData deleteInfo) { var accessLevelGetter = GetAccessLevel(deleteInfo.AdminUserName).Content; int accessLevel = (int)((ObjectContent)accessLevelGetter).Value; if (accessLevel == 2) { try { using (NipsDBEntities entities = new NipsDBEntities()) { Ledger ledgerEntry = entities.Ledgers.FirstOrDefault(l => l.LedgerID == deleteInfo.LedgerID); User getterObj = entities.Users.FirstOrDefault(u => u.ID == ledgerEntry.GetterID); getterObj.TotalPoints -= ledgerEntry.Amount; getterObj.SemesterPoints -= ledgerEntry.Amount; getterObj.WeekPoints -= ledgerEntry.Amount; entities.Ledgers.Remove(entities.Ledgers.FirstOrDefault(l => l.LedgerID == deleteInfo.LedgerID)); entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.Accepted)); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } } else { return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } }
public HttpResponseMessage ChangeAccessLevel(AccessLevelData accessData) { var accessLevelGetter = GetAccessLevel(accessData.AdminUserName).Content; int accessLevel = (int)((ObjectContent)accessLevelGetter).Value; if (accessLevel == 2) { try { using (NipsDBEntities entities = new NipsDBEntities()) { var lastName = accessData.BrotherName.Split(',')[0]; var firstName = accessData.BrotherName.Split(',')[1]; var brother = entities.Users.FirstOrDefault(u => u.LastName == lastName && u.FirstName == firstName); brother.AccessLevel = accessData.NewAccessLevel; entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK)); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } } else { return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } }
public void Get() { try { using (NipsDBEntities entities = new NipsDBEntities()) { List <User> users = entities.Users.ToList(); users = users.OrderBy(u => u.WeekPoints).ToList(); List <User> retList = new List <User>(); if (users.Count >= 3) { retList.Add(users[0]); retList.Add(users[1]); retList.Add(users[2]); User edge = users[2]; int i = 3; while (users.ElementAt(i).WeekPoints == edge.WeekPoints) { retList.Add(users[i]); } } else { retList = users; } StringBuilder sb = new StringBuilder(); sb.AppendLine("This week's top brothers are:"); foreach (User u in retList) { sb.AppendLine(u.LastName + ',' + u.FirstName); } sb.AppendLine("Please congratulate them!"); string body = sb.ToString(); MailMessage message = new MailMessage(); message.To.Add("*****@*****.**"); message.Subject = "This week's top brothers"; message.From = new MailAddress("*****@*****.**"); message.Body = body; var client = new SmtpClient(); client.EnableSsl = true; client.Send(message); foreach (User u in users) { u.WeekPoints = 0; } entities.SaveChanges(); return; } } catch (Exception ex) { return; } }
public HttpResponseMessage Post(Ledger ledgerEntry) { try { using (NipsDBEntities entities = new NipsDBEntities()) { string giver = ledgerEntry.GiverID; string getter = ledgerEntry.GetterID; int amount = ledgerEntry.Amount; string reason = ledgerEntry.Reason; var giverId = entities.AspNetUsers.FirstOrDefault(u => u.UserName == giver).Id; var ledgerKey = 0; try { ledgerKey = entities.Ledgers.Select(l => l.LedgerID).Max() + 1; } catch (Exception ex) { ledgerKey = 0; } var giverAccessLevel = entities.Users.FirstOrDefault(u => u.ID == giverId).AccessLevel; if (giverAccessLevel > 0) { var lastName = getter.Split(',')[0]; var firstName = getter.Split(',')[1]; var getterObj = entities.Users.FirstOrDefault(u => u.LastName == lastName && u.FirstName == firstName); getterObj.TotalPoints += amount; getterObj.SemesterPoints += amount; getterObj.WeekPoints += amount; ledgerEntry.LedgerID = ledgerKey; ledgerEntry.GiverID = giverId; ledgerEntry.GetterID = getterObj.ID; entities.Ledgers.Add(ledgerEntry); entities.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.Created)); } else { return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }
public HttpResponseMessage Post(User user) { try { using (NipsDBEntities entities = new NipsDBEntities()) { entities.Users.Add(user); entities.SaveChanges(); var message = Request.CreateResponse(HttpStatusCode.Created, user); message.Headers.Location = new Uri(Request.RequestUri + user.ID.ToString()); return(message); } } catch (Exception ex) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex)); } }