private AddUserCourseResult AddUserToCourses(ApplicationUser user, string stripePaymentId) { var result = new AddUserCourseResult(); try { //Create the UserCourse record so we know what the user has bought foreach (CartLine line in _cart.CartLines) { var usercourse = new UserCourse() { StripePaymentId = stripePaymentId, UserId = user.Id, CourseId = line.Product.Id }; _repository.AddUserToCourse(usercourse); } } catch (Exception ex) { result.ErrorMessage = ex.Message; result.Succeeded = false; } result.Succeeded = true; return(result); }
public UserCourse CreateCourse(int userId, int topicId, int nTasks, int durationDays) { if (database.UserCourses.Find(c => c.UserId == userId && c.EndDate > DateTime.Now).Count() > 0) { throw new Exception("User has unfinished course."); } var newUserCourse = new UserCourse { UserId = userId, EndDate = DateTime.Now.AddDays(durationDays) }; var tasksForTopic = database.Tasks.Find(t => t.TopicId == topicId).ToList(); var rand = new Random(); for (int i = 0; i < nTasks; ++i) { var randomTask = tasksForTopic[rand.Next(0, tasksForTopic.Count)]; tasksForTopic.Remove(randomTask); var newCourseTask = new CourseTask { TaskId = randomTask.Id, CompPer = 0 }; newUserCourse.CourseTasks.Add(newCourseTask); } database.UserCourses.Create(newUserCourse); database.Save(); return(newUserCourse); }
public async Task <IActionResult> AddUser(string CourseId) { int ok = 1; UserCourse userCourse = new UserCourse(); var userId = userManager.GetUserId(HttpContext.User); var authenticatedUser = await userManager.FindByIdAsync(userId) as MyUser; userCourse.UserId = userId; userCourse.CourseId = CourseId; var userCourses = await _context.UserCourses .Where(u => u.CourseId.Equals(CourseId)).ToListAsync(); foreach (var user in userCourses) { if (user.UserId.Equals(userId) && user.CourseId.Equals(CourseId)) { ok = 0; return(View("AlreadyEnrolled")); } } if (ok == 1) { authenticatedUser.NoOfCourses++; var course = await _context.Courses.FirstOrDefaultAsync(c => c.Id.Equals(CourseId)); authenticatedUser.Budget -= course.Price; await _context.UserCourses.AddAsync(userCourse); await _context.SaveChangesAsync(); } return(View("EnrollSuccessfully")); }
internal bool SetUsedHours(string userId, string courseName, float usedHours) { User user = userDataAccess.GetByIdOrSlug(userId); Course course = Context.Set <Course>().FirstOrDefault(x => x.Name == courseName); if (user == null || course == null) { return(false); } UserCourse userCourse = Context.Set <UserCourse>().FirstOrDefault(x => x.UserId == user.Id && x.CourseId == course.Id) ?? new UserCourse { UserId = user.Id, CourseId = course.Id }; userCourse.UsedHours = usedHours; try { userCourseDataAccess.Save(userCourse); return(true); } catch (Exception) { return(false); } }
public async Task <IActionResult> PutUserCourse(int id, UserCourse userCourse) { if (id != userCourse.UserId) { return(BadRequest()); } _context.Entry(userCourse).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserCourseExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <Boolean> dropStudentCourseRegistration(UserCourse userCourseToDrop) { var userCourseRegistration = _classDbContext.Remove(userCourseToDrop); await _classDbContext.SaveChangesAsync(); return(userCourseRegistration.State.Equals(Microsoft.EntityFrameworkCore.EntityState.Modified)); }
public async Task <ActionResult> ActiveCode(CodeForActiveDto codeForActiveDto) { var code = await _repo.GetCode(codeForActiveDto.CodeID); if (code != null) { if (code.Status == true) { var codeCourse = await _repo.GetCodesCourse(code.CodeID); foreach (var item in codeCourse) { UserCourse userCourse = new UserCourse { CourseId = item.CourseId, UserId = codeForActiveDto.UserId, CreatedAt = DateTime.Now }; _repo.Add(userCourse); await _repo.SaveAll(); } var code1 = code; code1.Status = false; _mapper.Map(code1, code); if (await _repo.SaveAll()) { return(Ok()); } } } return(BadRequest("fail")); }
public async Task <bool> RegisterToCourse(Guid courseId, Guid studentId) { try { var semester = await(from u in _context.Users where u.Id == studentId select u.CurrentSemester).FirstOrDefaultAsync(); var newRegister = new UserCourse() { Id = Guid.NewGuid(), Active = true, CourseId = courseId, UserId = studentId, Created = DateTime.Now, Semester = semester }; _context.UserCourses.Add(newRegister); await _context.SaveChangesAsync(); return(true); } catch (Exception e) { System.ArgumentException argEx = new System.ArgumentException("Exception", "An error occured on the database", e); throw argEx; } }
public async Task <IActionResult> Edit(int id, [Bind("Id,UserProfileId,CourseId,DateEnrolled,Score")] UserCourse userCourse) { if (id != userCourse.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(userCourse); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserCourseExists(userCourse.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(userCourse)); }
public UserCourse AddUserCourse(UserCourse userCourse) { User user = _context.Users .FirstOrDefault(u => u.Id == userCourse.UserId); if (user == null) { throw new ArgumentNullException(nameof(user)); } Course course = _context.Courses.FirstOrDefault(c => c.CourseId == userCourse.CourseId); if (course == null) { throw new ArgumentNullException(nameof(course)); } UserCourse uscr = new UserCourse() { Course = course, User = user }; _context.UserCourses.Add(uscr); _context.SaveChanges(); return(uscr); }
public IActionResult Edit([FromBody] UserCourse vm) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { repository.Update(vm); var result = Db.SaveChanges(); if (result != null) { return(Ok(result)); } else { return(BadRequest()); } } catch (ArgumentException ex) { return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message)); } }
public async Task <UserCourse> InsertUserCourse(UserCourse userCourse) { context.UserCourses.Add(userCourse); await context.SaveChangesAsync(); return(userCourse); }
public async Task <Boolean> registerStudentForCourse(UserCourse UserCourseToRegister) { var userCourseRegistration = _classDbContext.Add(UserCourseToRegister); await _classDbContext.SaveChangesAsync(); return(userCourseRegistration.State.Equals(Microsoft.EntityFrameworkCore.EntityState.Modified)); }
internal bool CompleteClass(string userId, string courseName, int classNumber) { User user = userDataAccess.GetByIdOrSlug(userId); Course course = Context.Set <Course>().FirstOrDefault(x => x.Name == courseName); if (user == null || course == null) { return(false); } UserCourse userCourse = Context.Set <UserCourse>().FirstOrDefault(x => x.UserId == user.Id && x.CourseId == course.Id) ?? new UserCourse { UserId = user.Id, CourseId = course.Id }; var value = (float)Math.Ceiling((decimal)classNumber / (decimal)course.Classes.Count * 100); userCourse.Progression = Math.Max(userCourse.Progression, Math.Min(value, 100)); try { userCourseDataAccess.Save(userCourse); return(true); } catch (Exception) { return(false); } }
public UserBO Enroll(int courseId, int userId) { using (var uow = _facade.UnitOfWork) { var userFromDb = uow.UserRepo.Get(userId); var courseFromDb = uow.CourseRepo.Get(courseId); if (userFromDb.Courses != null) { foreach (var course in userFromDb.Courses) { if (course.CourseId == courseFromDb.Id) { return(_userConv.Convert(userFromDb)); } } } var userCourse = new UserCourse(); userCourse.Course = courseFromDb; userCourse.CourseId = courseFromDb.Id; userCourse.User = userFromDb; userCourse.UserID = userFromDb.Id; userFromDb.Courses.Add(userCourse); uow.Complete(); return(_userConv.Convert(userFromDb)); } }
public IActionResult OnGet(string userid, int courseid) { UserCourse = _reader.Get <UserCourse>(userid, courseid); ViewData["Email"] = _db.GetUser(userid).Email; ViewData["CourseTitle"] = _reader.Get <Course>(courseid).Result.Title; return(Page()); }
public ResponseState RemoveCourse(int userId, int courseId) { bool userFind = this.repository.Any <User>(x => x.Id == userId); if (userFind == true) { UserCourse userCourse = this.repository.FirstOrDefault <UserCourse>(x => x.Id == userId && x.CourseId == courseId); if (userCourse != null) { this.repository.Delete <UserCourse>(userCourse); this.repository.SaveChanges(); return(new ResponseState { State = true, Massage = "OK" }); } return(new ResponseState { State = false, Massage = "CourseIsAbsent" }); } return(new ResponseState { State = false, Massage = "UserNotFound" }); }
public ResponseState UserCourseUpdate(UserCourse userCourse) { bool userFind = this.repository.Any <User>(x => x.Id == userCourse.Id); if (userFind == true) { var entity = this.repository.FirstOrDefault <UserCourse>(x => x.Id == userCourse.Id && x.CourseId == userCourse.CourseId); if (entity != null) { entity.IsComplete = userCourse.IsComplete; this.repository.Update <UserCourse>(entity); this.repository.SaveChanges(); return(new ResponseState { State = true, Massage = "OK" }); } return(new ResponseState { State = false, Massage = "EntityNotFound" }); } return(new ResponseState { State = false, Massage = "UserNotFound" }); }
public async Task <IActionResult> Edit(string originalUserId, int originalCourseId, [Bind("UserId", "CourseId")] UserCourse userCourse) { if (originalUserId == null || originalCourseId.Equals(default(int))) { return(NotFound()); } var originalUserCourse = await _db.UserCourses.SingleOrDefaultAsync(c => c.CourseId.Equals(originalCourseId) && c.UserId.Equals(originalUserId)); if (!UserCourseExists(userCourse.UserId, userCourse.CourseId)) { try { _db.Remove(originalUserCourse); _db.Add(userCourse); await _db.SaveChangesAsync(); return(RedirectToAction("Index")); } catch { ModelState.AddModelError("", "Unable to save changes"); } } ViewData["CourseId"] = new SelectList(_db.Courses, "Id", "Title"); ViewData["UserId"] = new SelectList(_userStore.Users, "Id", "Email"); return(View(userCourse)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,UserID,CourseID")] UserCourse userCourse) { if (id != userCourse.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(userCourse); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserCourseExists(userCourse.Id)) { return(NotFound()); } else { throw; } } ViewData["ShowMessage"] = "עדכון קורס התבצעה בהצלחה"; return(RedirectToAction(nameof(Index))); } ViewData["CourseID"] = new SelectList(_context.Course, "Id", "LecturerEmail", userCourse.CourseID); ViewData["UserID"] = new SelectList(_context.Users, "Id", "Id", userCourse.UserID); return(View(userCourse)); }
public JsonResult Edit(UserCourse userCourse) { userCourse.UpdateCourseNames(); var result = UpdateUserOwnedEntity(userCourse); return(result); }
public IActionResult CreateInvoice(UserCourse userCourse) { var invoiceId = _repo.CreateNewInvoice(userCourse.UserId); _lineItemRepository.CreateNewInvoiceLineItem(invoiceId, userCourse.CourseId); return(Ok(invoiceId)); }
private string CreateProblemMessage(UserCourse uc) { string message = "<p>There was a problem adding the user to the course. Their payment has been successful and so they should be added to the course.</p>"; message += string.Format("<p>You can do this in the Admin Section manually. The payment Id you need to update is {0}</<p>", uc.StripePaymentId); return(message); }
public ActionResult DeleteConfirmed(int id) { UserCourse userCourse = db.UserCourses.Find(id); db.UserCourses.Remove(userCourse); db.SaveChanges(); return(RedirectToAction("Index")); }
public async void UnassignUserCourse(int userId, int courseId) { UserCourse userCourse = await _context.UserCourses.FindAsync(userId, courseId); if (userCourse != null) { Remove(userCourse); } }
public ActionResult Payment(int idfield) { UserCourse uc = new UserCourse(); if (idfield != 0) { uc = db.UserCourses.Where(x => x.Id.Equals(idfield)).SingleOrDefault(); } return(View(uc)); }
public ActionResult Edit([Bind(Include = "UserId,CourseID,CourseName,CourseImg,CourseDes,CoursePrice")] UserCourse userCourse) { if (ModelState.IsValid) { db.Entry(userCourse).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(userCourse)); }
private async Task InsertUserCourse(int userId, int courseId) { var userCourse = new UserCourse() { CourseId = courseId, UserId = userId, CompletionDate = DateTime.UtcNow }; await courseRepo.InsertUserCourse(userCourse); }
public IActionResult UpdateUserCourse(UserCourse userCourse) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _courseRepo.UpdateCourse(userCourse); return(Ok()); }
public async Task <IActionResult> Create([Bind("Id,UserProfileId,CourseId,DateEnrolled,Score")] UserCourse userCourse) { if (ModelState.IsValid) { _context.Add(userCourse); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(userCourse)); }