Example #1
0
        public async Task <IActionResult> Post([FromBody] TaskItemAPI task)
        {
            var taskItem = new TaskItem()
            {
                Done        = task.Done,
                Id          = Guid.NewGuid(),
                TimeAdded   = DateTime.Now,
                Description = task.Description
            };

            context.Add(taskItem);
            await context.SaveChangesAsync();

            return(Ok(taskItem.Id));
        }
Example #2
0
            public async Task <int> Handle(UpdateClassCommand request, CancellationToken cancellationToken)
            {
                Class currentClass = await _context.Classes.Where(a => a.Id == request.Id).FirstOrDefaultAsync(cancellationToken);

                if (currentClass == null)
                {
                    throw new NotFoundException($"Class with id: {request.Id} is not found");
                }

                if (currentClass.Status == (int)ClassStatus.InProgress)
                {
                    throw new Exception($"Class with id: {request.Id} can not update it because in-progress");
                }

                bool isFound = await _context.Classes.AnyAsync(a => a.Name == request.Name && a.Id != request.Id);

                if (isFound)
                {
                    throw new KeyIsAlreadyExistsException($"this class: {request.Name} already found befory");
                }

                currentClass.Name      = request.Name;
                currentClass.StartTime = request.StartTime;
                currentClass.EndTime   = request.EndTime;

                await _context.SaveChangesAsync(cancellationToken);

                return(currentClass.Id);
            }
Example #3
0
            public async Task <int> Handle(ChangeStatusClassCommand request, CancellationToken cancellationToken)
            {
                TimeSpan timeNow      = DateTime.Now.TimeOfDay;
                Class    currentClass = await _context.Classes.Include(a => a.ClassInstructors).Where(a => a.Id == request.Id).FirstOrDefaultAsync(cancellationToken);

                if (currentClass == null)
                {
                    throw new NotFoundException($"Class with id: {request.Id} is not found");
                }

                bool isInProgressTime = TimeSpan.Compare(timeNow, currentClass.StartTime) >= 0 && TimeSpan.Compare(timeNow, currentClass.EndTime) <= 0;

                if (!currentClass.ClassInstructors.Any())
                {
                    throw new Exception("Can't Start this class because not have any instructors");
                }


                if (currentClass.Status == (int)ClassStatus.InProgress && isInProgressTime)
                {
                    throw new Exception($"Class with id: {request.Id} can not stop because it in-progress");
                }

                if (currentClass.Status == (int)ClassStatus.Stop && !isInProgressTime)
                {
                    throw new Exception($"Class with id: {request.Id} can not start because its not time to start");
                }

                currentClass.Status = isInProgressTime ? (int)ClassStatus.InProgress : (int)ClassStatus.Stop;
                await _context.SaveChangesAsync(cancellationToken);

                return(currentClass.Id);
            }
            public async Task <int> Handle(DeleteInstructorCommand request, CancellationToken cancellationToken)
            {
                Instructor currentInstructor = await _context.Instructors.Where(a => a.Id == request.Id).FirstOrDefaultAsync(cancellationToken);

                if (currentInstructor == null)
                {
                    throw new NotFoundException($"Instructor with id: {request.Id} is not found");
                }

                await DeleteInstructorClasses(request, cancellationToken);

                _context.Instructors.Remove(currentInstructor);
                await _context.SaveChangesAsync(cancellationToken);

                return(currentInstructor.Id);
            }
            public async Task <int> Handle(CreateInstructorCommand request, CancellationToken cancellationToken)
            {
                bool isFound = await _context.Instructors.AnyAsync(a => a.Mobile == request.Mobile);

                if (isFound)
                {
                    throw new KeyIsAlreadyExistsException($"this mobile: {request.Mobile} already found befory");
                }
                Instructor newInstructor = new Instructor
                {
                    Name   = request.Name,
                    Mobile = request.Mobile,
                };

                _context.Instructors.Add(newInstructor);
                await _context.SaveChangesAsync(cancellationToken);

                return(newInstructor.Id);
            }
            public async Task <int> Handle(DeleteClassCommand request, CancellationToken cancellationToken)
            {
                Class currentClass = await _context.Classes.Include(a => a.ClassInstructors).Where(a => a.Id == request.Id).FirstOrDefaultAsync(cancellationToken);

                if (currentClass == null)
                {
                    throw new NotFoundException($"Class with id: {request.Id} is not found");
                }

                if (currentClass.Status == (int)ClassStatus.InProgress)
                {
                    throw new Exception($"Class with id: {request.Id} can not Delete it because in-progress");
                }
                currentClass.ClassInstructors.Clear();
                _context.Classes.Remove(currentClass);
                await _context.SaveChangesAsync(cancellationToken);

                return(currentClass.Id);
            }
            public async Task <int> Handle(CreateClassCommand request, CancellationToken cancellationToken)
            {
                bool isFound = await _context.Classes.AnyAsync(a => a.Name == request.Name);

                if (isFound)
                {
                    throw new KeyIsAlreadyExistsException($"this class: {request.Name} already found befory");
                }
                Class newClass = new Class
                {
                    Name      = request.Name,
                    StartTime = request.StartTime,
                    EndTime   = request.EndTime
                };

                _context.Classes.Add(newClass);
                await _context.SaveChangesAsync(cancellationToken);

                return(newClass.Id);
            }
Example #8
0
            public async Task <int> Handle(UpdateInstructorCommand request, CancellationToken cancellationToken)
            {
                Instructor currentInstructor = await _context.Instructors.Where(a => a.Id == request.Id)
                                               .FirstOrDefaultAsync(cancellationToken);

                if (currentInstructor == null)
                {
                    throw new NotFoundException($"Instructor with id: {request.Id} is not found");
                }

                bool isFound = await _context.Instructors.AnyAsync(a => a.Mobile == request.Mobile && a.Id != request.Id);

                if (isFound)
                {
                    throw new KeyIsAlreadyExistsException($"this mobile: {request.Mobile} already found befory");
                }
                currentInstructor.Name   = request.Name;
                currentInstructor.Mobile = request.Mobile;
                await _context.SaveChangesAsync(cancellationToken);

                return(currentInstructor.Id);
            }
Example #9
0
            public async Task <int> Handle(AssignClassesToInstructorCommand request, CancellationToken cancellationToken)
            {
                Instructor currentInstructor = await _context.Instructors.Include(a => a.ClassInstructors).Where(a => a.Id == request.InstructorId).FirstOrDefaultAsync(cancellationToken);

                if (currentInstructor == null)
                {
                    throw new NotFoundException($"Instructor with id: {request.InstructorId} is not found");
                }

                await CheckClasses(request.ClassIds, cancellationToken);

                currentInstructor.ClassInstructors.Clear();
                request.ClassIds.ForEach(clas =>
                {
                    currentInstructor.ClassInstructors.Add(new ClassInstructor {
                        ClassId = clas
                    });
                });
                await _context.SaveChangesAsync(cancellationToken);

                return(currentInstructor.Id);
            }