Пример #1
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 }));
            }
        }
Пример #2
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);
        }