Example #1
0
        public async Task <Accountant> CreateAccountantAsync(AccountantRequest accountantrequest, CloudBlobContainer blobContainer, HttpPostedFile file)
        {
            var dbAccountant = await _context.Accountants.Where(h => (h.Surname == accountantrequest.Surname && h.Name == accountantrequest.Name)).ToArrayAsync();

            if (dbAccountant.Length > 0)
            {
                throw new RequestedResourceHasConflictException("code");
            }

            var            fileName = GetRandomBlobName(file.FileName);
            CloudBlockBlob blob     = blobContainer.GetBlockBlobReference(fileName);

            using (var fileStream = file.InputStream)
            {
                await blob.UploadFromStreamAsync(fileStream);
            }

            accountantrequest.Photo = blob.Uri.ToString();

            var accountant = Mapper.Map <AccountantRequest, Accountant>(accountantrequest);

            _context.Accountants.Add(accountant);

            await _context.SaveChangesAsync();

            return(accountant);
        }
        public async Task <Accountant> UpdateAccountantAsync(int accountantId, AccountantRequest updateAccountant, CloudBlobContainer blobContainer)
        {
            var dbAccountants = await _context.Accountants.Where(p => p.Name == updateAccountant.Name && p.Id != accountantId).ToArrayAsync();

            if (dbAccountants.Length > 0)
            {
                throw new RequestedResourceHasConflictException("code");
            }

            dbAccountants = _context.Accountants.Where(p => p.Id == accountantId).ToArray();
            if (dbAccountants.Length == 0)
            {
                throw new RequestedResourceNotFoundException();
            }

            var dbaccountant = dbAccountants[0];

            dbaccountant.Name    = updateAccountant.Name;
            dbaccountant.Surname = updateAccountant.Surname;
            dbaccountant.Age     = updateAccountant.Age;
            dbaccountant.ChiefId = updateAccountant.ChiefId;
            await _context.SaveChangesAsync();

            return(dbaccountant);
        }
Example #3
0
        public async Task <Accountant> UpdateAccountantAsync(int accountantId, AccountantRequest updateAccountant, CloudBlobContainer blobContainer, HttpPostedFile file)
        {
            var dbAccountants = await _context.Accountants.Where(p => p.Name == updateAccountant.Name && p.Id != accountantId).ToArrayAsync();

            if (dbAccountants.Length > 0)
            {
                throw new RequestedResourceHasConflictException("code");
            }

            dbAccountants = _context.Accountants.Where(p => p.Id == accountantId).ToArray();
            if (dbAccountants.Length == 0)
            {
                throw new RequestedResourceNotFoundException();
            }

            var dbaccountant = dbAccountants[0];

            var            fileName = GetRandomBlobName(file.FileName);
            CloudBlockBlob blob     = blobContainer.GetBlockBlobReference(fileName);

            using (var fileStream = file.InputStream)
            {
                await blob.UploadFromStreamAsync(fileStream);
            }

            string filename = Path.GetFileName(dbaccountant.Photo);

            var blobDelete = blobContainer.GetBlockBlobReference(filename);
            await blobDelete.DeleteIfExistsAsync();

            dbaccountant.Photo   = blob.Uri.ToString();
            dbaccountant.Name    = updateAccountant.Name;
            dbaccountant.Surname = updateAccountant.Surname;
            dbaccountant.Age     = updateAccountant.Age;
            dbaccountant.ChiefId = updateAccountant.ChiefId;
            await _context.SaveChangesAsync();

            return(dbaccountant);
        }