Ejemplo n.º 1
0
        public ActionResult ApproveOrders()
        {
            //OrdersPendingIndexView viewModel = new OrdersPendingIndexView();

            string username = User.Identity.Name;                                                            // get current logged in user

            Supervisor supervisor = db.Supervisors.SingleOrDefault(s => s.UserProfile.UserName == username); // gets supervisor from username

            // query retrieves orders which have not been checked by project supervisor
            var query = from o in db.Orders.Include("Student").Include("Student.Project").Include("Components")
                        where (o.Student.Project.Supervisor.SupervisorID == supervisor.SupervisorID && o.IsChecked == false) // display orders for students for supervisor
                        select o;

            // checks each order if they are above budget and if they are then marks the boolean as true and saves to db
            foreach (Order o in query)
            {
                if (o.OrderTotal > o.Student.Project.Budget)
                {
                    o.IsOverBudget    = true;
                    db.Entry(o).State = EntityState.Modified;
                }
            }

            db.SaveChanges();

            // view model.Ienumberalbe = query

            return(View(query));
        }
        /// <summary>
        /// Update map information
        /// </summary>
        /// <param name="mapModel"></param>
        /// <returns></returns>
        public void UpdateMap(MapModel mapModel)
        {
            if (string.IsNullOrWhiteSpace(mapModel.HashId))
            {
                throw HttpResponseExceptionHelper.Create("No map hash id specified", HttpStatusCode.BadRequest);
            }

            using (var db = new SystemDBContext())
            {
                var map = db.Maps.FirstOrDefault(x => x.HashId == mapModel.HashId);

                if (map == null)
                {
                    throw HttpResponseExceptionHelper.Create("No map correspond to the given hashId",
                                                             HttpStatusCode.BadRequest);
                }

                // Update info
                if (!string.IsNullOrWhiteSpace(mapModel.Content))
                {
                    map.Content = mapModel.Content;
                }
                map.Level = mapModel.Level;

                map.UpdateTime = TimeHelper.CurrentCanadaTime();

                // Mark entity as modified
                db.Entry(map).State = System.Data.Entity.EntityState.Modified;
                // call SaveChanges
                db.SaveChanges();
            }
        }
        public ActionResult Index()
        {
            string username = User.Identity.Name; // get current logged in user

            Student    student    = db.Students.SingleOrDefault(s => s.UserProfile.UserName == username);
            Supervisor supervisor = db.Supervisors.Include("UserProfile").FirstOrDefault(s => s.UserProfile.UserName == username);  // check logged in username vs name on table
            Technician technician = db.Technicians.Include("UserProfile").SingleOrDefault(s => s.UserProfile.UserName == username); // check logged in username vs name on table

            if (student != null)
            {
                return(RedirectToRoute("Student"));
            }

            else if (technician != null)
            {
                return(RedirectToRoute("Technician"));
            }

            else if (supervisor != null)
            {
                // get update project details with the right supervisor details
                foreach (Project project in db.Projects.Include("Supervisor").Include("Supervisor.UserProfile"))
                {
                    // modify fields
                    if (project.Supervisor.UserProfile.Email == supervisor.UserProfile.Email && project.Supervisor.FirstName == null)
                    {
                        Supervisor  oldSupervisor            = project.Supervisor;
                        UserProfile oldSupervisorUserProfile = project.Supervisor.UserProfile;
                        project.Supervisor                       = supervisor;           // update the project supervisor details
                        db.Entry(oldSupervisor).State            = EntityState.Deleted;  // delete old supervisor details
                        db.Entry(oldSupervisorUserProfile).State = EntityState.Deleted;  // delete old supervisor user profile//
                        db.Entry(project).State                  = EntityState.Modified; // update project details
                    }
                }
                db.SaveChanges();
                return(RedirectToRoute("Supervisor"));
            }

            else if (User.IsInRole("Admin"))
            {
                return(RedirectToRoute("Admin"));
            }

            return(View());
        }
        public ActionResult EnableAccount(int id)
        {
            UserProfile user = db.UserProfiles.Find(id); // retreives user

            user.IsChecked  = true;
            user.IsVerifyed = true;

            if (ModelState.IsValid)
            {
                db.Entry(user).State = EntityState.Modified; //saves changes of new state to db
                db.SaveChanges();
            }

            return(RedirectToAction("ReactivateAccount")); // refreshes the reactivate view
        }
Ejemplo n.º 5
0
        public void Update <T>(T entity) where T : class
        {
            DbEntityEntry entityEntry = dataContext.Entry(entity);

            if (entityEntry.State == EntityState.Detached)
            {
                dataContext.Set <T>().Attach(entity);
                entityEntry.State = EntityState.Modified;
            }
        }
Ejemplo n.º 6
0
        public ActionResult ViewNotNotifiedOrderedOrders(int id)
        {
            Order order = db.Orders.Find(id);

            if (order.StudentIsNotified != true)              // if the student is not notified then send an email to notify
            {
                order.StudentIsNotified = true;               // set status to true
                SendOrderReadyToCollectEmail(order.OrderID);  // send the email
                db.Entry(order).State = EntityState.Modified; // save the order object changes
                db.SaveChanges();

                return(RedirectToAction("ViewNotNotifiedOrderedOrders")); // refresh view
            }
            else
            {
                //send email method here
                SendOrderReadyToCollectEmail(order.OrderID);        // send email
                return(RedirectToAction("ViewNotCollectedOrders")); // send to orders not collected view
            }
        }
Ejemplo n.º 7
0
 public async Task <bool> UpdateUserAsync(User user)
 {
     _Context.users.Attach(user);
     _Context.Entry(user).State = EntityState.Modified;
     try
     {
         return(await _Context.SaveChangesAsync() > 0 ? true : false);
     }
     catch (Exception exp)
     {
         _Logger.LogError($"Error in {nameof(UpdateUserAsync)}: " + exp.Message);
     }
     return(false);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// 更新實體
 /// </summary>
 /// <param name="entity">實體</param>
 public TEntity Update(TEntity entity)
 {
     _dbContext.Set <TEntity>().Attach(entity);
     _dbContext.Entry(entity).State = EntityState.Modified;
     return(entity);
 }
Ejemplo n.º 9
0
        public async Task <Bill> AddNewBillAsync(BillResponse billResponse)
        {
            Bill bill = new Bill();

            if (billResponse.groupId != 0)
            {
                bill.groupId = billResponse.groupId;
            }
            bill.bill_created_at   = billResponse.bill_created_at;
            bill.bill_created_byId = billResponse.bill_created_byId;
            bill.bill_date         = billResponse.bill_date;
            bill.bill_updated_at   = billResponse.bill_updated_at;
            bill.description       = billResponse.description;
            bill.total_amount      = billResponse.total_amount;
            bill.bill_updated_byId = billResponse.bill_updated_byId;
            _Context.bills.Add(bill);
            try
            {
                await _Context.SaveChangesAsync();
            }
            catch (Exception exp)
            {
                _Logger.LogError($"Error in {nameof(AddNewBillAsync)}: " + exp.Message);
            }


            //int id = bill.billid;
            foreach (var x in billResponse.payers)
            {
                Payer payer = new Payer();
                payer.billId      = bill.billid;
                payer.paid_byId   = x.paid_byId;
                payer.amount_paid = x.amount_paid;
                _Context.payers.Add(payer);

                try
                {
                    await _Context.SaveChangesAsync();
                }
                catch (Exception exp)
                {
                    _Logger.LogError($"Error in {nameof(AddNewBillAsync)}: " + exp.Message);
                }
            }

            foreach (var x in billResponse.sharedwiths)
            {
                SharedWith sharedWith = new SharedWith();
                sharedWith.billId        = bill.billid;
                sharedWith.shared_withId = x.shared_withId;
                sharedWith.owes_amount   = x.owes_amount;
                _Context.sharedWiths.Add(sharedWith);

                try
                {
                    await _Context.SaveChangesAsync();
                }
                catch (Exception exp)
                {
                    _Logger.LogError($"Error in {nameof(AddNewBillAsync)}: " + exp.Message);
                }
            }

            foreach (var x in billResponse.settlements)
            {
                if (billResponse.groupId != 0)
                {
                    Settlements settlements = new Settlements();
                    settlements = await _Context.settlements
                                  .SingleOrDefaultAsync(c => c.groupId == billResponse.groupId &&
                                                        c.payeeId == x.payeeId && c.payerId == x.payerId);

                    if (settlements != null)
                    {
                        settlements.payerId = x.payerId;
                        settlements.payeeId = x.payeeId;
                        settlements.amount  = settlements.amount + x.paid_amount;
                        settlements.groupId = billResponse.groupId;
                        _Context.settlements.Attach(settlements);
                        _Context.Entry(settlements).State = EntityState.Modified;
                    }
                    else
                    {
                        settlements = await _Context.settlements
                                      .SingleOrDefaultAsync(c => c.groupId == billResponse.groupId &&
                                                            c.payeeId == x.payerId && c.payerId == x.payeeId);

                        if (settlements != null)
                        {
                            if (settlements.amount > x.paid_amount)
                            {
                                settlements.payerId = x.payeeId;
                                settlements.payeeId = x.payerId;
                                settlements.groupId = billResponse.groupId;
                                settlements.amount  = settlements.amount - x.paid_amount;
                                _Context.settlements.Attach(settlements);
                                _Context.Entry(settlements).State = EntityState.Modified;
                            }
                            else
                            {
                                settlements.payerId = x.payerId;
                                settlements.payeeId = x.payeeId;
                                settlements.groupId = billResponse.groupId;
                                settlements.amount  = x.paid_amount - settlements.amount;
                                _Context.settlements.Attach(settlements);
                                _Context.Entry(settlements).State = EntityState.Modified;
                            }
                        }
                        else
                        {
                            Settlements settlements1 = new Settlements();
                            settlements1.payerId = x.payerId;
                            settlements1.payeeId = x.payeeId;
                            settlements1.amount  = x.paid_amount;
                            settlements1.groupId = billResponse.groupId;
                            _Context.settlements.Add(settlements1);
                        }
                    }
                }
                else
                {
                    Settlements settlements = new Settlements();
                    settlements = await _Context.settlements
                                  .FirstOrDefaultAsync(c => c.groupId == bill.groupId &&
                                                       c.payeeId == x.payeeId && c.payerId == x.payerId);

                    if (settlements != null)
                    {
                        settlements.payerId = x.payerId;
                        settlements.payeeId = x.payeeId;
                        settlements.amount  = settlements.amount + x.paid_amount;
                        _Context.settlements.Attach(settlements);
                        _Context.Entry(settlements).State = EntityState.Modified;
                    }
                    else
                    {
                        settlements = await _Context.settlements
                                      .FirstOrDefaultAsync(c => c.groupId == bill.groupId &&
                                                           c.payeeId == x.payerId && c.payerId == x.payeeId);

                        if (settlements != null)
                        {
                            if (settlements.amount > x.paid_amount)
                            {
                                settlements.payerId = x.payeeId;
                                settlements.payeeId = x.payerId;
                                settlements.amount  = settlements.amount - x.paid_amount;
                                _Context.settlements.Attach(settlements);
                                _Context.Entry(settlements).State = EntityState.Modified;
                            }
                            else
                            {
                                settlements.payerId = x.payerId;
                                settlements.payeeId = x.payeeId;
                                settlements.amount  = x.paid_amount - settlements.amount;
                                _Context.settlements.Attach(settlements);
                                _Context.Entry(settlements).State = EntityState.Modified;
                            }
                        }
                        else
                        {
                            Settlements settlements1 = new Settlements();
                            settlements1.payerId = x.payerId;
                            settlements1.payeeId = x.payeeId;
                            settlements1.amount  = x.paid_amount;
                            //  settlements1.groupId = billResponse.groupId;
                            _Context.settlements.Add(settlements1);
                        }
                    }
                }
                try
                {
                    await _Context.SaveChangesAsync();
                }
                catch (Exception exp)
                {
                    _Logger.LogError($"Error in {nameof(AddNewBillAsync)}: " + exp.Message);
                }
            }

            for (int i = 0; i < billResponse.sharedwiths.Count; i++)
            {
                for (int j = i + 1; j < billResponse.sharedwiths.Count; j++)
                {
                    Friend friend = new Friend();

                    var frdexist = _Context.friends.SingleOrDefault(
                        c => (c.userId == billResponse.sharedwiths[i].shared_withId && c.friendId == billResponse.sharedwiths[j].shared_withId) ||
                        (c.userId == billResponse.sharedwiths[j].shared_withId && c.friendId == billResponse.sharedwiths[i].shared_withId));
                    if (frdexist == null)
                    {
                        friend.friendId = billResponse.sharedwiths[j].shared_withId;
                        friend.userId   = billResponse.sharedwiths[i].shared_withId;

                        _Context.friends.Add(friend);
                        try
                        {
                            await _Context.SaveChangesAsync();
                        }
                        catch (Exception exp)
                        {
                            _Logger.LogError($"Error in {nameof(AddNewBillAsync)}: " + exp.Message);
                        }
                    }
                }
            }


            return(bill);
        }
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                // Attempt to register the user

                try
                {
                    string confirmationToken =
                        WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Email = model.Email }, true);

                    SendEmailConfirmation(model.Email, model.UserName, confirmationToken);

                    if (model.SelectedVal.Contains("0"))
                    {
                        UserProfile userProfile = db.UserProfiles.FirstOrDefault(s => s.UserName == model.UserName);
                        Student     student     = new Student {
                            FirstName = model.FirstName, LastName = model.LastName, MiddleName = model.MiddleName, UserProfile = userProfile
                        };
                        userProfile.UserType = "Student";

                        if (ModelState.IsValid)
                        {
                            db.Entry <UserProfile>(userProfile).State = EntityState.Modified;
                            db.Students.Add(student);
                            db.SaveChanges();
                        }
                    }

                    // if dropdown list for supervisor is selected then do the below
                    else if (model.SelectedVal.Contains("1"))
                    {
                        UserProfile userProfile = db.UserProfiles.FirstOrDefault(s => s.UserName == model.UserName);
                        userProfile.Email = userProfile.Email.ToLower();
                        Supervisor supervisor = new Supervisor {
                            FirstName = model.FirstName, LastName = model.LastName, MiddleName = model.MiddleName, UserProfile = userProfile
                        };
                        userProfile.UserType = "Supervisor";
                        if (ModelState.IsValid)
                        {
                            db.Entry <UserProfile>(userProfile).State = EntityState.Modified;
                            db.Supervisors.Add(supervisor);
                            db.SaveChanges();
                        }
                    }

                    else if (model.SelectedVal.Contains("2"))
                    {
                        UserProfile userProfile = db.UserProfiles.FirstOrDefault(s => s.UserName == model.UserName);
                        Technician  technician  = new Technician {
                            FirstName = model.FirstName, LastName = model.LastName, MiddleName = model.MiddleName, UserProfile = userProfile
                        };
                        userProfile.UserType = "Technician";
                        if (ModelState.IsValid)
                        {
                            db.Entry <UserProfile>(userProfile).State = EntityState.Modified;
                            db.Technicians.Add(technician);
                            db.SaveChanges();
                        }
                    }



                    return(RedirectToAction("RegisterStepTwo", "Account"));
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // redisplay form, something is wrong if we got this far
            return(View(model));
        }
Ejemplo n.º 11
0
 public int Insert(TEntity entity)
 {
     dbcontext.Entry <TEntity>(entity).State = EntityState.Added;
     return(dbcontext.SaveChanges());
 }