Пример #1
0
        public async Task <bool> DeleteContainer(string containerName)
        {
            try
            {
                var           member = User.Identity.Name;
                MemberLicense ml     = this.iMemberLicenseRepository.MyLicense(member);
                // if (CloudStorageAccount.TryParse(config.Value.StorageConnection, out CloudStorageAccount storageAccount))
                if (CloudStorageAccount.TryParse(ml?.AzureSaConnectionString, out CloudStorageAccount storageAccount))
                {
                    CloudBlobClient    BlobClient = storageAccount.CreateCloudBlobClient();
                    CloudBlobContainer container  = BlobClient.GetContainerReference(containerName);

                    if (container != null)
                    {
                        await container.DeleteIfExistsAsync();

                        return(true);
                    }
                }
                else
                {
                    return(false);
                }
            }
            catch
            {
                return(false);
            }
            return(true);
        }
Пример #2
0
        public async Task <CloudContainersModel> ListContainers()
        {
            CloudContainersModel containersList = new CloudContainersModel();

            try
            {
                var           member = User.Identity.Name;
                MemberLicense ml     = this.iMemberLicenseRepository.MyLicense(member);
                if (CloudStorageAccount.TryParse(ml?.AzureSaConnectionString, out CloudStorageAccount storageAccount))
                {
                    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                    BlobContinuationToken continuationToken = null;
                    do
                    {
                        ContainerResultSegment resultSegment = await blobClient.ListContainersSegmentedAsync(continuationToken);

                        continuationToken = resultSegment.ContinuationToken;
                        containersList.AddRange(resultSegment.Results);
                    }while (continuationToken != null);
                }
            }
            catch
            {
            }
            return(containersList);
        }
Пример #3
0
        public async Task <CloudFile> InsertFile(IFormFile asset, string containerName)
        {
            try
            {
                var           member = User.Identity.Name;
                MemberLicense ml     = this.iMemberLicenseRepository.MyLicense(member);

                if (CloudStorageAccount.TryParse(ml?.AzureSaConnectionString, out CloudStorageAccount storageAccount))
                {
                    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                    CloudBlobContainer container = blobClient.GetContainerReference(containerName);

                    CloudBlockBlob blockBlob = container.GetBlockBlobReference(asset.FileName);
                    blockBlob.Metadata.Add("UploadedBy", member);
                    blockBlob.Metadata.Add("CreatedAt", DateTimeOffset.Now.ToString("O")); //2020-01-16T13:25:42.8408729+00:00

                    if (await blockBlob.ExistsAsync())
                    {
                        return(null);
                    }

                    MemberLicenseUsedStorage mlus = this.iMemberLicenseRepository.GetUsedStorage(ml.LicenseId);
                    long newSizeTestInBytes       = long.Parse(BigInteger.Add(mlus.AzureSaUsedSizeInBytes, asset.Length).ToString());
                    long capasityInBytes          = long.Parse(BigInteger.Multiply(ml.AzureSaSizeInGb, 1073741824).ToString());

                    if (newSizeTestInBytes <= capasityInBytes)
                    {
                        await blockBlob.UploadFromStreamAsync(asset.OpenReadStream());

                        // Increase Used Storage Size
                        //await container.FetchAttributesAsync();
                        mlus.AzureSaUsedSizeInBytes += asset.Length;
                        this.iMemberLicenseRepository.UpdateUsedStorage(mlus);
                        // End of Increase Used Storage Size

                        await blockBlob.FetchAttributesAsync();

                        CloudFile newFile = new CloudFile {
                            ContentType = blockBlob.Properties.ContentType, FileName = blockBlob.Name, URL = blockBlob.Uri.ToString(), Size = blockBlob.Properties.Length, UploadedBy = member, CreatedAt = DateTimeOffset.Now, ContainerName = containerName
                        };
                        return(newFile);
                    }
                    else
                    {
                        return(null);
                    }
                }
                else
                {
                    return(null);
                }
            }
            catch
            {
                return(null);
            }
        }
Пример #4
0
        public bool PrimaryAccessGranted(string username) // kurumsal(ekip) lisansı
        {
            MemberLicense myLicense = MemberLicenses.Where(ml => ml.Username == username && ml.EndDate > DateTimeOffset.Now && ml.LicenseType == "kurumsal").FirstOrDefault();

            if (myLicense != null) // if a license founded in my own or of a team(I joined) owner.
            {
                return(true);
            }
            return(false);
        }
Пример #5
0
        public IActionResult NewLicense([FromBody] MemberLicense memberLicense) //Accepts JSON body, not x-www-form-urlencoded!
        {
            ReturnModel newLic = iMemberLicenseRepository.NewLicense(memberLicense);

            if (newLic.ErrorCode == ErrorCodes.OK)
            {
                return(CreatedAtAction(null, null, (Guid)newLic.Model));           // 201 Created
            }
            return(new StatusCodeResult(StatusCodes.Status503ServiceUnavailable)); // 503 Service Unavailable Error.
        }
Пример #6
0
        public IActionResult DeleteLicense([FromRoute] string licenseId) //[FromRoute] is optional, it already accepts from route parameters but don't accepts JSON.
        {
            MemberLicense mLic = iMemberLicenseRepository.DeleteLicense(Guid.Parse(licenseId));

            if (mLic != null)
            {
                return(Ok(mLic)); // or use No Content without arguments returned.
            }
            return(NotFound());   // 404 resource not found, Microsoft docs use NotFound for this kind of behavior.
        }
Пример #7
0
        public MemberLicense DeleteLicense(Guid licenseId)
        {
            MemberLicense mLicense = context.MemberLicense.Where(ml => ml.LicenseId == licenseId).FirstOrDefault();

            if (mLicense != null)
            {
                context.MemberLicense.Remove(mLicense);
                context.SaveChanges();
            }
            return(mLicense); // This is to inform user.
        }
 /// <summary>
 /// Convert Corporate Entity  into Corporate Object
 /// </summary>
 ///<param name="model">MemberLicenseViewModel</param>
 ///<param name="CorporateEntity">DataAccess.Corporate</param>
 ///<returns>MemberLicenseViewModel</returns>
 public static MemberLicenseViewModel ToViewModel(this MemberLicense entity,
                                                  MemberLicenseViewModel model)
 {
     model.Id        = entity.Id;
     model.FinYearId = entity.FinYearId;
     model.MemberId  = entity.MemberId;
     model.MemberNo  = entity.Member.MemberNo;
     model.FullName  = entity.Member.Person.FullName;
     model.FinYear   = entity.FinYear.Name;
     model.LicenseNo = entity.LicenseNo;
     return(model);
 }
Пример #9
0
        public bool AccessGranted(string username) // herhangi bir lisans bulunuyor(kurumsal, bireysel veya ekip üyesi lisansı)
        {
            MemberLicense myLicense = MemberLicenses.Where(ml => ml.Username == username && ml.EndDate > DateTimeOffset.Now).FirstOrDefault();

            MemberLicense licenseJoined = context.TeamMember.Where(tm => tm.Username == username && tm.Status == true && tm.Team.Owner != username).Select(tm => tm.Team.OwnerNavigation).Select(m => m.MemberLicense).Where(ml => ml.EndDate > DateTimeOffset.Now).FirstOrDefault();

            if (myLicense != null || licenseJoined != null) // if a license founded in my own or of a team(I joined) owner.
            {
                return(true);
            }
            return(false);
        }
Пример #10
0
        [HttpGet("MyLicense")]               // GET MemberLicense/MyLicense
        public IActionResult MyLicense()     //Accepts from route parameters not JSON. You don't have to speficy [FromRoute], but you can..
        {
            var member = User.Identity.Name; // For security. From Claim(ClaimTypes.Name, Username) in JWT

            MemberLicense myLicense = iMemberLicenseRepository.MyLicense(member);

            if (myLicense != null)
            {
                return(Ok(myLicense));
            }
            return(NotFound());  // 404 resource not found, Microsoft docs use NotFound for this kind of behavior.
        }
Пример #11
0
        public async Task <bool> DeleteFile(string fileName, string containerName)
        {
            try
            {
                var           member = User.Identity.Name;
                MemberLicense ml     = this.iMemberLicenseRepository.MyLicense(member);
                // if (CloudStorageAccount.TryParse(config.Value.StorageConnection, out CloudStorageAccount storageAccount))
                if (CloudStorageAccount.TryParse(ml?.AzureSaConnectionString, out CloudStorageAccount storageAccount))
                {
                    CloudBlobClient    BlobClient = storageAccount.CreateCloudBlobClient();
                    CloudBlobContainer container  = BlobClient.GetContainerReference(containerName);

                    if (await container.ExistsAsync())
                    {
                        CloudBlob file = container.GetBlobReference(fileName);

                        if (await file.ExistsAsync())
                        {
                            await file.DeleteAsync();

                            //Decrease used storage
                            MemberLicenseUsedStorage mlus = this.iMemberLicenseRepository.GetUsedStorage(ml.LicenseId);
                            await container.FetchAttributesAsync();

                            // await file.FetchAttributesAsync();

                            mlus.AzureSaUsedSizeInBytes = long.Parse(BigInteger.Subtract(mlus.AzureSaUsedSizeInBytes, file.Properties.Length).ToString());


                            if (mlus.AzureSaUsedSizeInBytes < 0)
                            {
                                mlus.AzureSaUsedSizeInBytes = 0;
                            }

                            this.iMemberLicenseRepository.UpdateUsedStorage(mlus);
                            // End of Decrease Used Storage Size

                            return(true);
                        }
                    }
                }
                else
                {
                    return(false);
                }
            }
            catch
            {
                return(false);
            }
            return(true);
        }
Пример #12
0
        public async Task <SaveResult> SaveEntity(MemberLicenseViewModel viewModel)
        {
            SaveResult saveResult = new SaveResult();
            Dictionary <bool, string> dictionary = new Dictionary <bool, string>();

            var entity = new MemberLicense();

            try
            {
                if (viewModel.Id != 0)
                {
                    if (_context.MemberLicense.IgnoreQueryFilters().Any(a => a.Id == viewModel.Id))
                    {
                        entity = await _context.MemberLicense.IgnoreQueryFilters().FirstOrDefaultAsync(a => a.Id == viewModel.Id);
                    }
                    entity = viewModel.ToEntity(entity);
                    _context.MemberLicense.Update(entity);
                }
                else
                {
                    entity = viewModel.ToEntity(entity);
                    _context.MemberLicense.Add(entity);
                }

                await _context.SaveChangesAsync();

                if (entity.Id > 0)
                {
                    saveResult.IsSuccess = true;
                    saveResult.Id        = entity.Id;
                }
            }
            catch (DbUpdateException upDateEx)
            {
                var    results = upDateEx.GetSqlerrorNo();
                string msg     = results == (int)SqlErrNo.FK ? ConstEntity.MissingValueMsg : ConstEntity.UniqueKeyMsg;
                saveResult = dictionary.GetValidateEntityResults(msg).ToSaveResult();
            }
            catch (Exception ex)
            {
                saveResult.Message = CrudError.SaveErrorMsg;
            }


            return(saveResult);
        }
        /// <summary>
        /// Convert Corporate Object into Corporate Entity
        /// </summary>
        ///<param name="model">Corporate</param>
        ///<param name="CorporateEntity">DataAccess.Corporate</param>
        ///<returns>DataAccess.Corporate</returns>
        public static MemberLicense ToEntity(this MemberLicenseViewModel model, MemberLicense entity
                                             )
        {
            if (entity.Id == 0)
            {
                entity.CreatedUserId = model.SessionUserId;
            }
            else
            {
                entity.UpdatedUserId    = model.SessionUserId;
                entity.UpdatedTimestamp = DateTime.Now;
            }
            entity.FinYearId = model.FinYearId;
            entity.MemberId  = model.MemberId;
            entity.LicenseNo = model.LicenseNo;

            return(entity);
        }
Пример #14
0
        public async Task <IActionResult> DownloadFile(string fileName, string containerName)
        {
            try
            {
                MemoryStream  ms     = new MemoryStream();
                var           member = User.Identity.Name;
                MemberLicense ml     = this.iMemberLicenseRepository.MyLicense(member);
                // if (CloudStorageAccount.TryParse(config.Value.StorageConnection, out CloudStorageAccount storageAccount))
                if (CloudStorageAccount.TryParse(ml?.AzureSaConnectionString, out CloudStorageAccount storageAccount))
                {
                    CloudBlobClient    BlobClient = storageAccount.CreateCloudBlobClient();
                    CloudBlobContainer container  = BlobClient.GetContainerReference(containerName);

                    if (await container.ExistsAsync())
                    {
                        CloudBlob file = container.GetBlobReference(fileName);
                        if (await file.ExistsAsync())
                        {
                            await file.DownloadToStreamAsync(ms);

                            Stream blobStream = file.OpenReadAsync().Result;
                            // await file.FetchAttributesAsync();
                            return(File(blobStream, file.Properties.ContentType, file.Name));
                        }
                        else
                        {
                            return(Content("File does not exist"));
                        }
                    }
                    else
                    {
                        return(Content("Container does not exist"));
                    }
                }
                else
                {
                    return(Content("Error opening storage"));
                }
            }
            catch
            {
                return(Content("Error opening storage"));
            }
        }
Пример #15
0
        public MemberLicense MyLicense(string username)
        {
            MemberLicense myLicense = MemberLicenses.Where(ml => ml.Username == username && ml.EndDate > DateTimeOffset.Now).FirstOrDefault();

            MemberLicense licenseJoined = context.TeamMember.Where(tm => tm.Username == username && tm.Status == true && tm.Team.Owner != username).Select(tm => tm.Team.OwnerNavigation).Select(m => m.MemberLicense).Where(ml => ml.EndDate > DateTimeOffset.Now).FirstOrDefault();

            if (licenseJoined != null)
            {
                return(licenseJoined);
            }
            else if (myLicense != null)
            {
                return(myLicense);
            }
            else
            {
                return(null); // No any license.
            }
        }
Пример #16
0
        public async Task <CloudContainer> CreateContainer(string containerName)
        {
            try
            {
                var           member = User.Identity.Name;
                MemberLicense ml     = this.iMemberLicenseRepository.MyLicense(member);
                // if (CloudStorageAccount.TryParse(config.Value.StorageConnection, out CloudStorageAccount storageAccount))
                if (CloudStorageAccount.TryParse(ml?.AzureSaConnectionString, out CloudStorageAccount storageAccount))
                {
                    CloudBlobClient    BlobClient = storageAccount.CreateCloudBlobClient();
                    CloudBlobContainer container  = BlobClient.GetContainerReference(containerName);

                    container.Metadata.Add("CreatedBy", member);
                    container.Metadata.Add("CreatedAt", DateTimeOffset.Now.ToString());

                    if (await container.ExistsAsync())
                    {
                        return(null);
                    }
                    else
                    {
                        await container.CreateAsync();

                        CloudContainer newContainer = new CloudContainer
                        {
                            ContainerName = container.Name,
                            URI           = container.Uri.ToString(),
                            CreatedAt     = DateTimeOffset.Now,
                            CreatedBy     = member
                        };
                        return(newContainer);
                    }
                }
                else
                {
                    return(null);
                }
            }
            catch
            {
                return(null);
            }
        }
Пример #17
0
        public ReturnModel NewLicense(MemberLicense memberLicense)
        {
            try
            {
                context.MemberLicense.Add(memberLicense);

                MemberLicenseUsedStorage mlus = new MemberLicenseUsedStorage {
                    LicenseId = memberLicense.LicenseId, AzureSaUsedSizeInBytes = 1
                };
                context.MemberLicenseUsedStorage.Add(mlus);

                context.SaveChanges();
            }
            catch { return(new ReturnModel {
                    ErrorCode = ErrorCodes.DatabaseError
                }); }
            return(new ReturnModel {
                ErrorCode = ErrorCodes.OK, Model = memberLicense.LicenseId
            });                                                                                    // Provides TeamId(identity autoset from db)
        }
Пример #18
0
        public async Task <CloudFilesModel> ListFiles(string containerName)
        {
            CloudFilesModel blobsList = new CloudFilesModel(containerName);

            try
            {
                var           member = User.Identity.Name;
                MemberLicense ml     = this.iMemberLicenseRepository.MyLicense(member);
                // if (CloudStorageAccount.TryParse(config.Value.StorageConnection, out CloudStorageAccount storageAccount))
                if (CloudStorageAccount.TryParse(ml?.AzureSaConnectionString, out CloudStorageAccount storageAccount))
                {
                    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

                    CloudBlobContainer    container             = blobClient.GetContainerReference(containerName);
                    BlobContinuationToken blobContinuationToken = null;
                    // var results = await container.ListBlobsSegmentedAsync(null, blobContinuationToken);
                    do
                    {
                        BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(
                            prefix : null,
                            useFlatBlobListing : true,
                            blobListingDetails : BlobListingDetails.All, // none
                            maxResults : null,
                            currentToken : blobContinuationToken,
                            options : null,
                            operationContext : null
                            );

                        blobContinuationToken = resultSegment.ContinuationToken;
                        blobsList.AddRange(resultSegment.Results, containerName);
                    } while (blobContinuationToken != null); // Loop while the continuation token is not null.
                }
            }
            catch
            {
            }
            return(blobsList);
        }
Пример #19
0
        public bool CanSendMoreTeamMemberRequest(string thisMember)
        {
            //lisanstaki kişi sayısı(maxi team member capasity)
            MemberLicense myLicense = context.MemberLicense.Where(ml => ml.Username == thisMember && ml.EndDate > DateTimeOffset.Now).FirstOrDefault();

            if (myLicense == null)
            {
                return(false);
            }

            int maximumEmployees = myLicense.NumberOfEmployees;

            //team members count in all teams.   //for unique, GroupBy(ptr => ptr.PrivateTalkId).Select(group => group.First())
            int allUniqueTeamMembersRequests = context.Team.Where(t => t.Owner == thisMember).SelectMany(m => m.TeamMember).Select(tm => tm.Username).GroupBy(username => username).Select(group => group.First()).Count();

            if (allUniqueTeamMembersRequests < maximumEmployees) // if still can add team member, return true,
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }