Пример #1
0
        public IActionResult Index(ElementViewModel model)
        {
            if (ModelState.IsValid)
            {
                _elementDataService.Add(new Element
                {
                    Name         = model.Name,
                    Description  = model.Description,
                    Price        = model.Price,
                    Type         = model.Type,
                    Symbol       = model.Symbol,
                    AtomicNumber = model.AtomicNumber
                });
                var succes = _elementDataService.Commit();
                _mailService.SendMessage("*****@*****.**", model.Name, $"Element added: {model.Description}");
                ViewBag.UserMessage = $"Mail sent for new element: {model.Name} with price: {model.Price} and decription: {model.Description}";
                ModelState.Clear();
            }

            IEnumerable <Element> _elements = GetElements();

            var viewModel = new ElementsViewModel()
            {
                Elements = _elements
            };


            return(View(viewModel));
        }
Пример #2
0
        public IActionResult Post([FromBody] ElementOccurrenceViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var element = _mapper.Map <ElementOccurrenceViewModel, ElementOccurrence>(model);

                    _elementDataService.Add(element);
                    if (_elementDataService.Commit())
                    {
                        var result = _mapper.Map <ElementOccurrence, ElementOccurrenceViewModel>(element);

                        return(Created($"api/apielements/{element.Id}", result));
                    }
                    ;
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save element: {ex}");
            }

            return(BadRequest("Failed to save element"));
        }
Пример #3
0
        public async Task <IActionResult> Post([FromBody] ElementViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //var element = new Element
                    //{
                    //    Name = elementViewModel.Name,
                    //    AtomicNumber = elementViewModel.AtomicNumber,
                    //    Description = elementViewModel.Description,
                    //    Symbol = elementViewModel.Symbol,
                    //    Price = elementViewModel.Price,
                    //    Type = elementViewModel.Type
                    //};
                    Element element = _mapper.Map <ElementViewModel, Element>(model);

                    var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

                    element.User = currentUser;

                    _elementDataService.Add(element);
                    if (_elementDataService.Commit())
                    {
                        //ElementViewModel result = new ElementViewModel()
                        //{
                        //    ElementId = element.Id,
                        //    AtomicNumber = element.AtomicNumber,
                        //    Symbol = element.Symbol,
                        //    Type = element.Type,
                        //    Price = element.Price,
                        //    Name = element.Name,
                        //    Description = element.Description
                        //};
                        var result = _mapper.Map <Element, ElementViewModel>(element);

                        return(Created($"api/apielements/{element.Id}", result));
                    }
                    ;
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save element: {ex}");
            }

            return(BadRequest("Failed to save element"));
        }