Beispiel #1
0
        public async Task <FileEntity?> DoAddAsync(IFormFile file, CancellationToken cancellationToken)
        {
            var fileData = new byte[file.Length];

            await using var fileStream = file.OpenReadStream();
            await fileStream.ReadAsync(fileData, cancellationToken)
            .ConfigureAwait(false);

            var fileEntity = new FileEntity {
                Name       = file.FileName,
                Format     = file.ContentType,
                Size       = fileData.Length,
                Data       = fileData,
                UploadedOn = DateTimeOffset.Now
            };

            var fileSizeInMegaByte = fileEntity.Size * 0.001 * 0.001;

            if (fileSizeInMegaByte > 256)
            {
                return(null);
            }

            await _dbContext.Files
            .AddAsync(fileEntity, cancellationToken)
            .ConfigureAwait(false);

            await _dbContext.SaveChangesAsync(cancellationToken)
            .ConfigureAwait(false);

            return(fileEntity);
        }
        public async Task DoAddAsync(NotificationEntity notification, CancellationToken cancellationToken)
        {
            await _dbContext.Notifications
            .AddAsync(notification, cancellationToken)
            .ConfigureAwait(false);

            await _dbContext
            .SaveChangesAsync(cancellationToken)
            .ConfigureAwait(false);
        }
Beispiel #3
0
        public async Task <IActionResult> SignUpAsync(
            [FromBody][Required] UserAdditionDto newUser,
            CancellationToken cancellationToken)
        {
            _logger.Log(LogLevel.Information, $"Starting sign up with name: {newUser.UserName}");

            var result = await _userManager.IsUserExistsAsync(newUser.UserName, cancellationToken);

            if (result)
            {
                _logger.Log(LogLevel.Information, $"Username: ({newUser.UserName}) already exists");
                ModelState.AddModelError("Username", "Username already exists");
                return(BadRequest(ModelState));
            }

            var now = DateTimeOffset.Now;

            var mappedUser = _mapper.Map <UserEntity>(newUser);

            mappedUser.About      = string.Empty;
            mappedUser.CreatedOn  = now;
            mappedUser.ModifiedOn = now;

            await _userManager.CreateAsync(mappedUser).ConfigureAwait(false);

            await _userManager.AddPasswordAsync(mappedUser, newUser.Password)
            .ConfigureAwait(false);

            await _userManager.AddToRolesAsync(mappedUser, new List <string> {
                Roles.WorkoutManager,
                Roles.PostManager,
                Roles.CommentManager,
                Roles.MessageSender,
            });

            var emailToken = await _userManager.GenerateEmailConfirmationTokenAsync(mappedUser)
                             .ConfigureAwait(false);

            emailToken = System.Web.HttpUtility.UrlEncode(emailToken);
            var confirmationLink = $"{Request.Scheme}://{Request.Host.Value}/email-confirmation?userId={mappedUser.Id}&token={emailToken}";

            _emailSender.SendEmail(
                mappedUser.Email, "Confirm your email",
                $"<h3>Hi, {mappedUser.UserName}!</h3> <br> Please confirm your account by <a href={confirmationLink}>clicking here</a>.");

            await _dbContext.SaveChangesAsync(cancellationToken)
            .ConfigureAwait(false);

            _logger.Log(LogLevel.Information, $"Signed up with name: {newUser.UserName}");

            return(Ok());
        }
        public async Task DoAddAsync(
            FeedbackEntity feedback,
            CancellationToken cancellationToken)
        {
            await _dbContext.Feedbacks
            .AddAsync(feedback, cancellationToken)
            .ConfigureAwait(false);

            await _dbContext
            .SaveChangesAsync(cancellationToken)
            .ConfigureAwait(false);
        }
Beispiel #5
0
        public async Task <UserEntity?> DoUpdateAsync(UserEntity currentUser, UpdateUserDto updateUserDto,
                                                      CancellationToken cancellationToken)
        {
            _mapper.Map(updateUserDto, currentUser);

            currentUser.ModifiedOn = DateTimeOffset.Now;

            await _dbContext
            .SaveChangesAsync(cancellationToken)
            .ConfigureAwait(false);

            var newlyFetchedUser = await _userManager.FindByIdWithAdditionalDataAsync(
                currentUser.Id, includesFollowsData : false, includesProfilePicture : true, cancellationToken : cancellationToken)
                                   .ConfigureAwait(false);

            return(newlyFetchedUser);
        }
        public async Task <MessageEntity> DoAddAsync(MessageEntity message, CancellationToken cancellationToken)
        {
            var createdMessage = await _dbContext.Messages
                                 .AddAsync(message, cancellationToken)
                                 .ConfigureAwait(false);

            await _dbContext
            .SaveChangesAsync(cancellationToken)
            .ConfigureAwait(false);

            return(await _dbContext.Messages
                   .Where(_ => _.Id == createdMessage.Entity.Id)
                   .Include(_ => _.SenderUser)
                   .ThenInclude(_ => _.ProfilePicture)
                   .Include(_ => _.TriggeredUser)
                   .ThenInclude(_ => _.ProfilePicture)
                   .FirstOrDefaultAsync(cancellationToken)
                   .ConfigureAwait(false));
        }
Beispiel #7
0
        public async Task DoAddAsync(WorkoutEntity workout, IReadOnlyCollection <int> fileIds, CancellationToken cancellationToken)
        {
            await _dbContext.Workouts
            .AddAsync(workout, cancellationToken)
            .ConfigureAwait(false);

            await _dbContext
            .SaveChangesAsync(cancellationToken)
            .ConfigureAwait(false);

            if (fileIds.Count > 0)
            {
                var createdWorkout = await _dbContext.Workouts
                                     .Where(_ => _.CreatedOn == workout.CreatedOn)
                                     .FirstOrDefaultAsync(cancellationToken)
                                     .ConfigureAwait(false);

                var newEntities = fileIds.Select(_ => new WorkoutFileRelationEntity {
                    WorkoutId = createdWorkout !.Id,
                    FileId    = _
                });
Beispiel #8
0
        public async Task DoAddAsync(PostEntity newPost, IReadOnlyCollection <int> fileIds, CancellationToken cancellationToken)
        {
            await _dbContext.Posts
            .AddAsync(newPost, cancellationToken)
            .ConfigureAwait(false); // TODO - store return value to get Id

            await _dbContext
            .SaveChangesAsync(cancellationToken)
            .ConfigureAwait(false);

            if (fileIds.Count > 0)
            {
                var createdPost = await _dbContext.Posts
                                  .Where(_ => _.PostedOn == newPost.PostedOn)
                                  .FirstOrDefaultAsync(cancellationToken)
                                  .ConfigureAwait(false);

                var newEntities = fileIds.Select(_ => new PostFileRelationEntity {
                    PostId = createdPost !.Id,
                    FileId = _
                });