Beispiel #1
0
        public async Task <IActionResult> ShareForUser([FromBody] ShareForUserIn shareForUserIn)
        {
            StatusCode <ShareForUserOut> status = await _shareService.ShareForUser(shareForUserIn, _userManager.GetUserName(User));

            if (status.Code == StatusCodes.Status404NotFound)
            {
                return(NotFound(status.Message));
            }

            return(Ok(status.Body));
        }
Beispiel #2
0
        public async Task <StatusCode <ShareForUserOut> > ShareForUser(ShareForUserIn shareForUserIn, string ownerUsername)
        {
            if (shareForUserIn.Username == ownerUsername)
            {
                return(new StatusCode <ShareForUserOut>(StatusCodes.Status404NotFound, $"You cannot share your own file with yourself"));
            }

            if (!await _databaseContext.Users.AnyAsync(_ => _.UserName == shareForUserIn.Username))
            {
                return(new StatusCode <ShareForUserOut>(StatusCodes.Status404NotFound, $"User {shareForUserIn.Username} not found"));
            }

            string ownerId = (await _databaseContext.Users
                              .FirstOrDefaultAsync(_ => _.UserName == ownerUsername))?
                             .Id;

            ResourceAbstract resourceAbstract = await _databaseContext.ResourceAbstracts
                                                .Include(_ => _.ShareForUsers)
                                                .FirstOrDefaultAsync(_ => _.ID == shareForUserIn.ResourceId && _.OwnerID == ownerId);

            if (resourceAbstract == null)
            {
                return(new StatusCode <ShareForUserOut>(StatusCodes.Status404NotFound, $"Resource {shareForUserIn.ResourceId} not found"));
            }

            string userId = (await _databaseContext.Users
                             .FirstOrDefaultAsync(_ => _.UserName == shareForUserIn.Username))?
                            .Id;

            ShareForUser shareForUser = await _databaseContext.ShareForUsers
                                        .FirstOrDefaultAsync(_ => _.OwnerID == ownerId && _.ResourceID == shareForUserIn.ResourceId && _.SharedForUserID == userId);

            if (shareForUser == null)
            {
                shareForUser = new ShareForUser
                {
                    CreatedDateTime    = DateTime.Now,
                    ExpirationDateTime = shareForUserIn.ExpirationDateTime,
                    OwnerID            = ownerId,
                    ResourceID         = shareForUserIn.ResourceId,
                    SharedForUserID    = userId
                };

                resourceAbstract.IsShared         = true;
                resourceAbstract.IsSharedForUsers = true;
                resourceAbstract.ShareForUsers.Add(shareForUser);
            }
            else
            {
                shareForUser.LastModifiedDateTime = DateTime.Now;
                shareForUser.ExpirationDateTime   = shareForUserIn.ExpirationDateTime;
            }

            if (shareForUser.ExpirationDateTime.HasValue)
            {
                shareForUser.ExpirationDateTime = shareForUser.ExpirationDateTime.Value.AddHours(1);
            }

            await _databaseContext.SaveChangesAsync();

            ShareForUserOut result = _mapper.Map <ShareForUserOut>(shareForUser);

            return(new StatusCode <ShareForUserOut>(StatusCodes.Status200OK, result));
        }