Ejemplo n.º 1
0
        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));
            }
        }
Ejemplo n.º 2
0
 public HttpResponseMessage Get(string userName)
 {
     try
     {
         using (NipsDBEntities entities = new NipsDBEntities())
         {
             var         id      = entities.AspNetUsers.FirstOrDefault(u => u.UserName == userName).Id;
             User        curUser = entities.Users.FirstOrDefault(u => u.ID == id);
             List <User> users   = entities.Users.ToList <User>();
             Dictionary <string, int> retDict = new Dictionary <string, int>();
             retDict.Add("totalPoints", curUser.TotalPoints);
             retDict.Add("semesterPoints", curUser.SemesterPoints);
             retDict.Add("weekPoints", curUser.WeekPoints);
             //retDict.Add("weekRank", 1);
             //retDict.Add("semesterRank", 1);
             retDict.Add("weekRank", getWeekRank(curUser, users));
             retDict.Add("semesterRank", getSemesterRank(curUser, users));
             return(Request.CreateResponse(HttpStatusCode.OK, retDict));
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
Ejemplo n.º 3
0
        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));
            }
        }
Ejemplo n.º 4
0
        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));
            }
        }
Ejemplo n.º 5
0
        public HttpResponseMessage GetLedger(string userName)
        {
            var accessLevelGetter = GetAccessLevel(userName).Content;
            int accessLevel       = (int)((ObjectContent)accessLevelGetter).Value;

            if (accessLevel == 2)
            {
                try
                {
                    using (NipsDBEntities entities = new NipsDBEntities())
                    {
                        List <Ledger> ledger = entities.Ledgers.ToList();
                        foreach (Ledger l in ledger)
                        {
                            l.GiverID  = IDtoName(l.GiverID);
                            l.GetterID = IDtoName(l.GetterID);
                        }
                        return(Request.CreateResponse(HttpStatusCode.OK, ledger));
                    }
                }
                catch (Exception ex)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
                }
            }
            else
            {
                return(Request.CreateResponse(HttpStatusCode.Unauthorized));
            }
        }
Ejemplo n.º 6
0
 private string IDtoName(string id)
 {
     using (NipsDBEntities entities = new NipsDBEntities())
     {
         string lastName  = entities.Users.FirstOrDefault(u => u.ID == id).LastName;
         string firstName = entities.Users.FirstOrDefault(u => u.ID == id).FirstName;
         return(lastName + ',' + firstName);
     }
 }
Ejemplo n.º 7
0
        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;
            }
        }
Ejemplo n.º 8
0
 public HttpResponseMessage Get()
 {
     try
     {
         using (NipsDBEntities entities = new NipsDBEntities())
         {
             List <User> users = entities.Users.ToList();
             return(Request.CreateResponse(HttpStatusCode.OK, users));
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
Ejemplo n.º 9
0
 public HttpResponseMessage GetAccessLevel(string userName)
 {
     try
     {
         using (NipsDBEntities entities = new NipsDBEntities())
         {
             var  id      = entities.AspNetUsers.FirstOrDefault(u => u.UserName == userName).Id;
             User curUser = entities.Users.FirstOrDefault(u => u.ID == id);
             return(Request.CreateResponse(HttpStatusCode.OK, curUser.AccessLevel));
         }
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
     }
 }
Ejemplo n.º 10
0
 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));
     }
 }
Ejemplo n.º 11
0
        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));
            }
        }
Ejemplo n.º 12
0
        public HttpResponseMessage GetPointsTable(string userName)
        {
            var accessLevelGetter = GetAccessLevel(userName).Content;
            int accessLevel       = (int)((ObjectContent)accessLevelGetter).Value;

            if (accessLevel == 2)
            {
                try
                {
                    using (NipsDBEntities entities = new NipsDBEntities())
                    {
                        List <RankFormattedUser> retList = new List <RankFormattedUser>();
                        List <User> users = entities.Users.ToList();
                        foreach (User u in users)
                        {
                            RankFormattedUser retUser = new RankFormattedUser
                            {
                                FirstName      = u.FirstName,
                                LastName       = u.LastName,
                                WeekPoints     = u.WeekPoints,
                                SemesterPoints = u.SemesterPoints,
                                TotalPoints    = u.TotalPoints,
                                WeekRank       = getWeekRank(u, users),
                                SemesterRank   = getSemesterRank(u, users),
                            };
                            retList.Add(retUser);
                        }
                        return(Request.CreateResponse(HttpStatusCode.OK, retList));
                    }
                }
                catch (Exception ex)
                {
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex));
                }
            }
            else
            {
                return(Request.CreateResponse(HttpStatusCode.Unauthorized));
            }
        }