Пример #1
0
        public void MassCreateComputerTypes()
        {
            var fakeComputerTypes = new List <ComputerTypeViewModel>();

            for (int i = 1; i < 6; i++)
            {
                var computerType = new ComputerTypeViewModel()
                {
                    ComputerTypeCode        = $"CTCODE{i}",
                    ComputerTypeName        = $"ComputerTypeName {i}",
                    ComputerTypeDescription = $"ComputerTypeDesription {i}",
                    Status = true
                };
                fakeComputerTypes.Add(computerType);
            }

            foreach (var computerTypeViewModel in fakeComputerTypes)
            {
                ComputerType newComputerType = new ComputerType();
                newComputerType.UpdateComputerType(computerTypeViewModel);

                _computerTypeService.Add(newComputerType);
                _computerTypeService.Save();
            }
        }
        public async Task <ActionResult <string> > PostComputerType(string name)
        {
            string cm;

            try
            {
                cm = await _computerTypeService.Add(name);
            }
            catch (DuplicateTypeException)
            {
                return(BadRequest());
            }
            catch (Exception)
            {
                return(BadRequest());
            }

            return(cm);
        }
Пример #3
0
        public HttpResponseMessage Post(HttpRequestMessage request, ComputerTypeViewModel computerTypeVm)
        {
            return(CreateHttpResponse(request, () =>
            {
                HttpResponseMessage response = null;
                if (!ModelState.IsValid)
                {
                    response = request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState.Values.FirstOrDefault()?.Errors.FirstOrDefault()?.ErrorMessage);
                }
                else
                {
                    var newComputerType = new ComputerType();
                    newComputerType.UpdateComputerType(computerTypeVm);

                    var computerType = _computerTypeService.Add(newComputerType);
                    _computerTypeService.Save();

                    response = request.CreateResponse(HttpStatusCode.Created, computerType);
                }
                return response;
            }));
        }