Example #1
0
        public IActionResult Create(CreateSubjectInputModel model)
        {
            if (ModelState.IsValid)
            {
                this.subjectService.CreateAsync(model);
            }

            return(this.Redirect("/Subjects/All"));
        }
        public async ASYNC.Task <CreateSubjectInputModel> GetTeachersFullNameWithId()
        {
            var teachers = await this.GetTeachers();

            var model = new CreateSubjectInputModel()
            {
                Teachers = teachers.ToDictionary(x => x.Id, x => $"{x.Title} {x.FirstName} {x.LastName}")
            };

            return(model);
        }
        public void Edit(CreateSubjectInputModel input, int id)
        {
            // Try to make it async
            var subjectToUpdate = this.GetById(id).Result;

            //Fix error handling
            try
            {
                subjectToUpdate.Name        = input.Name;
                subjectToUpdate.TeacherId   = input.TeacherId;
                subjectToUpdate.Description = input.Description;
            }
            catch (DbUpdateException ex)
            {
                Console.WriteLine(ex.Message);
            }

            this.context.Subjects.Update(subjectToUpdate);
            this.context.SaveChanges();
        }
        public void CreateAsync(CreateSubjectInputModel input)
        {
            // Fix error returning
            // Find out why SaveChangesAync doesn't work
            try
            {
                var subject = new Subject()
                {
                    Name        = input.Name,
                    TeacherId   = input.TeacherId,
                    Description = input.Description,
                };

                this.context.Subjects.Add(subject);
                this.context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
Example #5
0
        public IActionResult EditPost(CreateSubjectInputModel input, int id)
        {
            this.subjectService.Edit(input, id);

            return(RedirectToAction(nameof(All)));
        }