Esempio n. 1
0
        public async Task Update(int id, ModelItemRequest request)
        {
            var item = _dbContext.Models
                       .FirstOrDefaultAsync(x => x.Id == id);

            var brand = _dbContext.Brands
                        .AsNoTracking()
                        .FirstOrDefaultAsync(x => x.Id == request.BrandId);

            await Task.WhenAll(item, brand);

            if (item == null)
            {
                throw new InvalidModelException();
            }
            if (brand.Result == null)
            {
                throw new InvalidBrandException();
            }

            item.Result.BrandId = brand.Result.Id;
            item.Result.Name    = request.Name;

            await _dbContext.SaveChangesAsync();
        }
Esempio n. 2
0
        public async Task <int> Create(ModelItemRequest request)
        {
            var brand = _dbContext.Brands
                        .AsNoTracking()
                        .FirstOrDefaultAsync(x => x.Id == request.BrandId);

            await Task.WhenAll(brand);

            if (brand.Result == null)
            {
                throw new InvalidBrandException();
            }

            var newItem = new Model
            {
                BrandId = brand.Result.Id,
                Name    = request.Name,
            };

            if (await ModelExists(newItem))
            {
                throw new DuplicateModelException();
            }
            _dbContext.Add(newItem);
            await _dbContext.SaveChangesAsync();

            return(newItem.Id);
        }
Esempio n. 3
0
        public async Task <IActionResult> Update(int id, [FromBody] ModelItemRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                await _modelsRepository.Update(id, request);

                return(NoContent());
            }
            catch (InvalidBrandException)
            {
                string errorText = String.Format("Brand with ID: {0} doesn't exist", request.BrandId);
                return(StatusCode(StatusCodes.Status409Conflict, new { Message = errorText }));
            }
            catch (InvalidModelException)
            {
                return(NotFound());
            }
        }
Esempio n. 4
0
        public async Task <IActionResult> Create([FromBody] ModelItemRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                var itemId = await _modelsRepository.Create(request);

                return(CreatedAtAction(nameof(GetById), new { id = itemId }, itemId));
            }
            catch (InvalidBrandException)
            {
                string errorText = String.Format("Brand with ID: {0} doesn't exist", request.BrandId);
                return(StatusCode(StatusCodes.Status409Conflict, new { Message = errorText }));
            }
            catch (DuplicateModelException)
            {
                string errorText = String.Format("This model already exist", request.BrandId);
                return(StatusCode(StatusCodes.Status409Conflict, new { Message = errorText }));
            }
        }
Esempio n. 5
0
        public async Task <int> Create(CreateDeviceRequest request)
        {
            var office = _dbContext.Offices
                         .AsNoTracking()
                         .FirstOrDefaultAsync(x => x.Id == request.OfficeId);

            var brand = _dbContext.Brands
                        .AsNoTracking()
                        .FirstOrDefaultAsync(x => x.Id == request.BrandId);

            if (request.NewModel == true)
            {
                ModelItemRequest modelItem = new ModelItemRequest()
                {
                    Name    = request.ModelName,
                    BrandId = request.BrandId,
                };
                request.ModelId = await _modelsRepository.Create(modelItem);
            }

            var model = _dbContext.Models
                        .AsNoTracking()
                        .FirstOrDefaultAsync(x => x.Id == request.ModelId);

            await Task.WhenAll(office, brand, model);

            if (office.Result == null)
            {
                throw new InvalidOfficeException();
            }
            if (brand.Result == null)
            {
                throw new InvalidBrandException();
            }
            if (model.Result == null)
            {
                throw new InvalidModelException();
            }

            var newItem = new Device
            {
                BrandId           = brand.Result.Id,
                ModelId           = model.Result.Id,
                Available         = true,
                Active            = true,
                Image             = request.Image,
                UserId            = null,
                IdentificationNum = request.IdentificationNum,
                SerialNum         = request.SerialNum,
                OS        = request.OS,
                Purchased = request.Purchased,
                Vendor    = request.Vendor,
                TaxRate   = request.TaxRate,
                OfficeId  = office.Result.Id,
            };

            if (await DeviceWithSerialNumberExists(newItem))
            {
                throw new DuplicateDeviceSerialNumberException();
            }
            if (await DeviceWithIdentificationNumberExists(newItem))
            {
                throw new DuplicateDeviceIdentificationNumberException();
            }
            _dbContext.Add(newItem);
            await _dbContext.SaveChangesAsync();

            return(newItem.Id);
        }
Esempio n. 6
0
        public async Task Update(int id, UpdateDeviceRequest request)
        {
            var item = await _dbContext.Devices
                       .FirstOrDefaultAsync(x => x.Id == id);

            if (item == null)
            {
                throw new InvalidDeviceException();
            }

            var office = _dbContext.Offices
                         .AsNoTracking()
                         .FirstOrDefaultAsync(x => x.Id == request.OfficeId);

            var brand = _dbContext.Brands
                        .AsNoTracking()
                        .FirstOrDefaultAsync(x => x.Id == request.BrandId);

            if (request.NewModel == true)
            {
                ModelItemRequest modelItem = new ModelItemRequest()
                {
                    Name    = request.ModelName,
                    BrandId = request.BrandId,
                };
                request.ModelId = await _modelsRepository.Create(modelItem);
            }

            var model = _dbContext.Models
                        .AsNoTracking()
                        .FirstOrDefaultAsync(x => x.Id == request.ModelId);

            await Task.WhenAll(office, brand, model);

            if (office.Result == null)
            {
                throw new InvalidOfficeException();
            }
            if (brand.Result == null)
            {
                throw new InvalidBrandException();
            }
            if (model.Result == null)
            {
                throw new InvalidModelException();
            }

            item.BrandId           = brand.Result.Id;
            item.ModelId           = model.Result.Id;
            item.Available         = true;
            item.Image             = request.Image;
            item.IdentificationNum = request.IdentificationNum;
            item.SerialNum         = request.SerialNum;
            item.OS        = request.OS;
            item.Purchased = request.Purchased;
            item.Vendor    = request.Vendor;
            item.TaxRate   = request.TaxRate;
            item.OfficeId  = office.Result.Id;

            if (await DeviceWithSerialNumberExists(item))
            {
                throw new DuplicateDeviceSerialNumberException();
            }
            if (await DeviceWithIdentificationNumberExists(item))
            {
                throw new DuplicateDeviceIdentificationNumberException();
            }

            await _dbContext.SaveChangesAsync();
        }