Пример #1
0
        public async Task<Manager> UpdateManagerAsync(int managerId, ManagerRequest updateManager, CloudBlobContainer blobContainer, HttpPostedFile file)
        {
            var dbManagers = await _context.Managers.Where(m => m.Name == updateManager.Name && m.Id != managerId).ToArrayAsync();
            if (dbManagers.Length > 0)
            {
                throw new RequestedResourceHasConflictException("code");
            }

            dbManagers = _context.Managers.Where(p => p.Id == managerId).ToArray();
            if (dbManagers.Length == 0)
            {
                throw new RequestedResourceNotFoundException();
            }

            var dbmanager = dbManagers[0];

            var fileName = GetRandomBlobName(file.FileName);
            CloudBlockBlob blob = blobContainer.GetBlockBlobReference(fileName);
            using (var fileStream = file.InputStream)
            {
                await blob.UploadFromStreamAsync(fileStream);
            }

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

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

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

            return dbmanager;
        }
Пример #2
0
        public async Task<Manager> CreateManagerAsync(ManagerRequest managerRequest, CloudBlobContainer blobContainer, HttpPostedFile file)
        {
            var dbManager = await _context.Managers.Where(m => (m.Surname == managerRequest.Surname && m.Name == managerRequest.Name)).ToArrayAsync();
            if (dbManager.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);
            }

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

            var manager = Mapper.Map<ManagerRequest, Manager>(managerRequest);
            _context.Managers.Add(manager);

            await _context.SaveChangesAsync();

            return manager;
        }