コード例 #1
0
        public async Task CreateActivityAsync_CorrectUpdateOnContext(
            string teacher, string room, string subject, string classGroup, int slot)
        {
            using var context = PrepareData.GetDbContext(); var activityDTO = new ActivityCreateDTO()
            {
                ClassGroup = classGroup,
                Room       = room,
                Slot       = slot,
                Subject    = subject,
                Teacher    = teacher
            };
            var service = new ActivitiesService(context);
            var value   = await service.CreateActivityAsync(activityDTO);

            var activity = GetActivities(context).FirstOrDefault(
                a => a.Teacher.Name == teacher &&
                a.Subject.Name == subject &&
                a.Slot.Index == slot &&
                a.Room.Name == room &&
                a.ClassGroup.Name == classGroup
                );

            Assert.True(value.IsRight);
            Assert.NotNull(activity);
        }
コード例 #2
0
        public async Task <Either <ErrorDTO, Unit> > CreateActivityAsync(ActivityCreateDTO activity)
        {
            try {
                if (activity is null)
                {
                    return(Left(new ErrorDTO("Activity does not exists")));
                }

                if (!ValidateActivityForCreate(activity))
                {
                    return(Left(new ErrorDTO("One of values on this slot is occupied")));
                }

                var classGroup = await _context.ClassGroups.FirstOrDefaultAsync(c => c.Name == activity.ClassGroup);

                var subject = await _context.Subjects.FirstOrDefaultAsync(s => s.Name == activity.Subject);

                var teacher = await _context.Teachers.FirstOrDefaultAsync(t => t.Name == activity.Teacher);

                var slot = await _context.Slots.FirstOrDefaultAsync(s => s.Index == activity.Slot);

                var room = await _context.Rooms.FirstOrDefaultAsync(r => r.Name == activity.Room);

                var createdActivity = new Activity(room, classGroup, subject, slot, teacher);
                if (!ValidateUpdatedActivity(createdActivity))
                {
                    return(Left(new ErrorDTO("One of values is incorrect")));
                }


                await _context.Activities.AddAsync(createdActivity);

                await _context.SaveChangesAsync();

                return(Right(Unit.Default));
            }
            catch (Exception) {
                return(Left(new ErrorDTO("Error while creating activity")));
            }
        }
コード例 #3
0
        public bool Save(ActivityCreateDTO entity)
        {
            var activity = this.mapper.Map <Activity>(entity);

            activity.Uid         = Guid.NewGuid();
            activity.AddDateTime = DateTime.Now;
            var roles = new List <ActivitiesUsers>();

            if (entity.AuthorUids == null)
            {
                throw new ArgumentOutOfRangeException(nameof(entity.AuthorUids));
            }

            foreach (var item in entity.AuthorUids)
            {
                var user = this.userSimpleManager.Find()?.FirstOrDefault(a => a.Uid == item);

                if (user != null)
                {
                    roles.Add(new ActivitiesUsers
                    {
                        Uid          = Guid.NewGuid(),
                        ActivityGuid = activity.Uid,
                        UserGuid     = user.Uid,
                        UserType     = UserTypes.Organizer
                    });
                }
            }

            if (this.activitySimpleManager.Save(activity))
            {
                foreach (var item in roles)
                {
                    this.activitiesUsersSimpleManager.Save(item);
                }
            }

            return(true);
        }