public async Task <VMGetNodes> CreateAsync(VMAddNodes node) { if (!_validator.Validate(node.Name)) { throw new ArgumentException("Data not valid. Name must has maximum 30 chars."); } var level = _context.Nodes.Where(x => x.IdNode == node.ParentId).Select(x => x.Level).FirstOrDefault(); var newNode = new DbNode { Level = level + 1, Name = node.Name, ParentId = node.ParentId }; var result = await _context.Nodes.AddAsync(newNode); _context.SaveChanges(); var dbNode = result.Entity; var viewModel = new VMGetNodes(); viewModel.IdNode = dbNode.IdNode; viewModel.Level = dbNode.Level; viewModel.Name = dbNode.Name; viewModel.ParentId = dbNode.ParentId; return(viewModel); }
public async Task <IActionResult> Create([FromBody] VMAddNodes node) { try { var result = await _service.CreateAsync(node); return(CreatedAtAction(nameof(GetNodes), new { IdNode = result.IdNode }, result)); } catch (Exception) { return(BadRequest()); } }