예제 #1
0
        public async Task <IOutput> Handle(ChangePhotoInput request, CancellationToken cancellationToken)
        {
            var user = await _context.Users
                       .FirstOrDefaultAsync(x => x.Id == request.UserId, cancellationToken : cancellationToken);

            if (user is null)
            {
                return(ActionOutput.Error("Пользователь не найден"));
            }

            using var unit = _context.CreateUnitOfWork();

            try
            {
                var oldFile = await _context.AppFiles
                              .FirstOrDefaultAsync(x => x.Id == user.UserPhotoId, cancellationToken : cancellationToken);

                user.UserPhoto = null;

                var deleteResult = await _fileUploader.DeleteFile(oldFile.Path);
            }
            catch (Exception e) {}

            var fileSaveResult = await _fileUploader.SaveFile(request.NewPhoto);

            if (fileSaveResult.Succeeded == false)
            {
                return(ActionOutput.Error("Что-то пошло не так"));
            }
            var filePath        = fileSaveResult.Data.OperatedFilePath;
            var filePathRelated = fileSaveResult.Data.OperatedFileRelatedPath;
            var fileEntity      = new AppFile(request.NewPhoto.FileName, filePath, filePathRelated)
            {
                UserId = user.Id
            };
            var items = await _context.AppFiles.Where(x => x.UserId == user.Id).ToListAsync(cancellationToken);

            _context.AppFiles.RemoveRange(items);

            await _context.SaveChangesAsync(cancellationToken);

            await _context.AppFiles.AddAsync(fileEntity, cancellationToken);

            user.Photo = filePathRelated;

            await _context.SaveChangesAsync(cancellationToken);

            await unit.Apply();

            return(ActionOutput.SuccessData(user.Photo));
        }
        public async Task <IOutput> Handle(AddPracticeOrderInput request, CancellationToken cancellationToken)
        {
            var author = await _context.Users
                         .Include(x => x.UserProgresses)
                         .ThenInclude(x => x.Lesson)
                         .FirstOrDefaultAsync(x => x.Id == request.UserId, cancellationToken: cancellationToken);

            if (author is null)
            {
                return(ActionOutput.Error("Пользователь не был найден"));
            }

            var lesson = await _context.Lessons
                         .FirstOrDefaultAsync(x => x.Id == request.LessonId, cancellationToken : cancellationToken);

            if (lesson is null)
            {
                return(ActionOutput.Error("Урок не был найден"));
            }

            if (lesson.IsPractice == false)
            {
                return(ActionOutput.Error("Урок не практический"));
            }

            if (lesson.Index > 1)
            {
                var hasLastLesson = author.UserProgresses.Any(x => x.Lesson.Index == lesson.Index - 1);
                if (hasLastLesson == false)
                {
                    return(ActionOutput.Error("Прошлый урок не пройден"));
                }
            }

            /* check last order */
            var lastOrder = await _practiceOrderProvider.GetUserPracticeOrders(author.Id, lesson.Id);

            if (lastOrder.FirstOrDefault()?.IsDone == false)
            {
                return(ActionOutput.Error("Заявка уже оформлена"));
            }


            using var unit = _context.CreateUnitOfWork();

            var fileSaveResult = await _fileUploader.SaveFile(request.CodeFile);

            if (fileSaveResult.Succeeded == false)
            {
                return(ActionOutput.Error("Что-то пошло не так"));
            }

            var filePath        = fileSaveResult.Data.OperatedFilePath;
            var filePathRelated = fileSaveResult.Data.OperatedFileRelatedPath;
            var fileEntity      = new AppFile(request.CodeFile.FileName, filePath, filePathRelated);
            await _context.AppFiles.AddAsync(fileEntity, cancellationToken);

            var order = new Entity.PracticeOrder(author, lesson, fileEntity);
            await _context.PracticeOrders.AddAsync(order, cancellationToken);

            await _context.SaveChangesAsync(cancellationToken);

            await unit.Apply();

            return(ActionOutput.SuccessData(order.Id));
        }
예제 #3
0
        public async Task <IOutput> Handle(SignUpInput request, CancellationToken cancellationToken)
        {
            var hasSameNick = await _context.Users
                              .AnyAsync(x => x.Nick == request.Nick, cancellationToken : cancellationToken);

            if (hasSameNick)
            {
                return(ActionOutput.Error("Пользователь с таким ником уже зарегистрирован"));
            }

            var user = new Entity.User(request.Mail)
            {
                Nick = request.Nick
            };
            var registerResult = await _userManager.CreateAsync(user);

            if (registerResult.Succeeded == false)
            {
                return(ActionOutput.Error("Такой пользователь уже есть"));
            }

            await _context.SaveChangesAsync(cancellationToken);

            using var unit = _context.CreateUnitOfWork();

            var userPhotoPath = _configuration.GetSection("Static:DefaultUserPhoto").Value;

            if (request.UserPhoto != null)
            {
                var fileSaveResult = await _fileUploader.SaveFile(request.UserPhoto);

                if (fileSaveResult.Succeeded == false)
                {
                    return(ActionOutput.Error("Что-то пошло не так"));
                }
                var filePath        = fileSaveResult.Data.OperatedFilePath;
                var filePathRelated = fileSaveResult.Data.OperatedFileRelatedPath;
                var fileEntity      = new AppFile(request.UserPhoto.FileName, filePath, filePathRelated)
                {
                    UserId = user.Id,
                };
                userPhotoPath = filePathRelated;
                _context.AppFiles.Add(fileEntity);
            }

            user.Photo = userPhotoPath;

            await _userManager.AddToRoleAsync(user, UserRoles.Participant.ToString());

            await _userManager.AddPasswordAsync(user, request.Password);

            _logger.LogInformation($"User {user} was registered");

            await _context.SaveChangesAsync(cancellationToken);

            await unit.Apply();

            var identity = _dataProvider.GetIdentity(request.Mail);

            if (identity is null)
            {
                return(ActionOutput.Error("Данные не верны"));
            }

            return(ActionOutput.SuccessData(new { token = _dataProvider.GetJwtByIdentity(identity) }));
        }