Esempio n. 1
0
        public static Account SignUp(String name, string lastname, DateTime birthdate, string email, string password, Decimal deposit)
        {
            using (ElearnerContext dbContext = new ElearnerContext())
            {
                if (String.IsNullOrWhiteSpace(name) || String.IsNullOrWhiteSpace(lastname) ||
                    String.IsNullOrWhiteSpace(email) || String.IsNullOrWhiteSpace(password) || birthdate == null || deposit < 0)
                {
                    return(null);
                }
                else if (dbContext.Accounts.FirstOrDefault(a => a.Email == email) != null)
                {
                    return(null);
                }

                Account accountRecord = new Account()
                {
                    Email    = email,
                    Password = password,
                };

                dbContext.Accounts.Add(accountRecord);
                dbContext.SaveChanges();

                int lastInsertedId = dbContext.Accounts.OrderByDescending(a => a.Id).Select(a => a.Id).First();

                Student studentRecord = new Student()
                {
                    Name      = name,
                    Lastname  = lastname,
                    Birthdate = birthdate,
                    AccountId = lastInsertedId
                };

                dbContext.Students.Add(studentRecord);
                dbContext.SaveChanges();

                BankAccount bankAccount = new BankAccount()
                {
                    AccountId = lastInsertedId,
                    Deposit   = deposit
                };

                dbContext.BankAccounts.Add(bankAccount);
                dbContext.SaveChanges();

                Account result = new Account();
                result             = accountRecord;
                result.Student     = studentRecord;
                result.BankAccount = bankAccount;

                return(result);
            }
        }
Esempio n. 2
0
        public static void SaveSubscriptionChanges(int courseId, int accountId, string comment, byte?rating)
        {
            using (ElearnerContext dbContext = new ElearnerContext())
            {
                // If you have composite primary key, then pass key values in the order they defined in model:
                var subscriptionInDB = dbContext.Subscriptions.Find(courseId, accountId);

                if (!String.IsNullOrEmpty(comment))
                {
                    subscriptionInDB.Comment = comment;
                }

                if (rating != null)
                {
                    subscriptionInDB.Rate = rating;
                }

                try
                {
                    dbContext.SaveChanges();
                }
                catch (Exception)
                {
                    throw new Exception("could not reach the Database");
                }
            }
        }
Esempio n. 3
0
 public static void UpdateGradeToDb(byte?grade, int studentid, int courseid)
 {
     using (ElearnerContext dbContext = new ElearnerContext())
     {
         var query = dbContext.Subscriptions.Where(x => x.StudentId == studentid && x.CourseId == courseid).FirstOrDefault();
         query.Grade = grade;
         dbContext.SaveChanges();
     }
 }
Esempio n. 4
0
        public static string PurchaseCourse(int courseId, int accountid, decimal courseCost)
        {
            using (ElearnerContext dbContext = new ElearnerContext())
            {
                Subscription dbRecord = dbContext.Subscriptions
                                        .Where(c => c.CourseId == courseId)
                                        .Where(s => s.StudentId == accountid)
                                        .FirstOrDefault();

                if (dbRecord != null && dbRecord.Course.Price == 0.0m)
                {
                    return("Course is free");
                }
                else if (dbRecord != null)
                {
                    return("Course already has purchased!");
                }

                BankAccount currentUserDeposit = dbContext.BankAccounts.Where(b => b.AccountId == accountid).FirstOrDefault();

                if (currentUserDeposit.Deposit < courseCost)
                {
                    return("You dont have enough money!");
                }

                currentUserDeposit.Deposit -= courseCost;
                dbContext.SaveChanges();

                BankAccount teacherDeposit = dbContext.BankAccounts
                                             .Where(b => b.AccountId == (dbContext.Courses.Where(c => c.Id == courseId).Select(t => t.TeacherId).FirstOrDefault()))
                                             .FirstOrDefault();
                teacherDeposit.Deposit += courseCost;
                dbContext.SaveChanges();

                dbContext.Subscriptions.Add(new Subscription {
                    CourseId = courseId, StudentId = accountid
                });
                dbContext.SaveChanges();

                return("Course is successfully purchased!");
            }
        }
Esempio n. 5
0
        public static void UpdateStudentToDb(Student current, Student changes, ElearnerContext dbContext)
        {
            if (current == null && changes == null)
            {
                throw new ArgumentException("Accounts cannot be null.");
            }

            current.Name      = changes.Name;
            current.Lastname  = changes.Lastname;
            current.Birthdate = changes.Birthdate;

            dbContext.SaveChanges();
        }
Esempio n. 6
0
        public static decimal AddMoneyToUser(int accountId, decimal amount)
        {
            BankAccount bankAccount;

            using (ElearnerContext dbContext = new ElearnerContext())
            {
                bankAccount          = dbContext.BankAccounts.Where(b => b.AccountId == accountId).FirstOrDefault();
                bankAccount.Deposit += amount;

                dbContext.SaveChanges();
            }

            return(bankAccount.Deposit);
        }
Esempio n. 7
0
        public static void UpdateAccountToDb(Account current, Account changed)
        {
            if (current == null && changed == null)
            {
                throw new ArgumentException("Accounts cannot be null.");
            }

            using (ElearnerContext dbContext = new ElearnerContext())
            {
                current.Email    = changed.Email;
                current.Password = changed.Password;

                dbContext.SaveChanges();
            }
        }
Esempio n. 8
0
        public static void UpdateCourse(Course changedCourse)
        {
            using (ElearnerContext dbContext = new ElearnerContext())
            {
                Course oldCourse = dbContext.Courses.Where(c => c.Id == changedCourse.Id).Include(c => c.Content).FirstOrDefault();

                oldCourse.Name                = changedCourse.Name;
                oldCourse.Price               = changedCourse.Price;
                oldCourse.Description         = changedCourse.Description;
                oldCourse.Duration            = changedCourse.Duration;
                oldCourse.Content.TextContent = changedCourse.Content.TextContent;
                oldCourse.Content.VideoUrl    = changedCourse.Content.VideoUrl;

                dbContext.SaveChanges();
            }
        }
Esempio n. 9
0
        public static void AddCourseToDb(Account account, AddCourseViewModel addCourseViewModel)
        {
            using (ElearnerContext dbContext = new ElearnerContext())
            {
                dbContext.Courses.Add(new Course()
                {
                    Name        = addCourseViewModel.TeachingCourse.Name,
                    Duration    = addCourseViewModel.TeachingCourse.Duration,
                    Price       = addCourseViewModel.TeachingCourse.Price,
                    Description = addCourseViewModel.TeachingCourse.Description,
                    TeacherId   = account.Id,
                    Content     = addCourseViewModel.TeachingCourse.Content,
                    Questions   = addCourseViewModel.TeachingCourse.Questions
                });

                dbContext.SaveChanges();
            }
        }
Esempio n. 10
0
        public static void RemoveObjectToDb <T> (T obj) where T : class
        {
            if (obj == null)
            {
                throw new ArgumentException("Object cannot be null.");
            }

            using (ElearnerContext dbContext = new ElearnerContext())
            {
                try
                {
                    dbContext.Set <T>().Remove(obj);
                    dbContext.SaveChanges();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
        }
Esempio n. 11
0
        public static Account SignUpTeacher(SignUpTeacherViewModel teacherViewModel)
        {
            Account createdTeacher;

            if (teacherViewModel == null || string.IsNullOrWhiteSpace(teacherViewModel.TeacherAccount.Email) || string.IsNullOrWhiteSpace(teacherViewModel.TeacherAccount.Password) || teacherViewModel.TeacherAccount.BankAccount.Deposit < 0 ||
                string.IsNullOrWhiteSpace(teacherViewModel.TeachingCourse.Name) || string.IsNullOrWhiteSpace(teacherViewModel.TeachingCourse.Description) || teacherViewModel.TeachingCourse.Duration < 0)
            {
                return(null);
            }

            using (ElearnerContext dbContext = new ElearnerContext())
            {
                createdTeacher = dbContext.Accounts.Add(teacherViewModel.TeacherAccount);

                dbContext.Courses.Add(teacherViewModel.TeachingCourse);
                dbContext.SaveChanges();
            }

            return(createdTeacher);
        }