Esempio n. 1
0
 public ActionResult <Portfolio> Create([FromBody] Portfolio portfolio)
 {
     if (portfolio.UserId != User.GetId())
     {
         return(Conflict());
     }
     portfolio = PortfolioService.Create(portfolio);
     return(CreatedAtAction(nameof(GetById), new { id = portfolio.Id }, portfolio));
 }
        private void grdPortfolios_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            var grid     = (DataGridView)sender;
            var oldValue = grid[e.ColumnIndex, e.RowIndex].Value;
            var newValue = e.FormattedValue.ToString();

            //No change
            if (oldValue != null && oldValue.ToString().ExtEquals(newValue))
            {
                return;
            }

            //New row
            else if (oldValue == null && !string.IsNullOrEmpty(newValue))
            {
                if (!PortfolioService.Create(newValue))
                {
                    grid.CancelEdit();
                }
            }

            //Updating row
            else if (oldValue != null && !string.IsNullOrEmpty(newValue) && !oldValue.ToString().ExtEquals(newValue))
            {
                //Name is empty
                if (grid[0, e.RowIndex].Value == null || string.IsNullOrEmpty(grid[0, e.RowIndex].Value.ToString()))
                {
                    grid.CancelEdit();
                    return;
                }

                //Rename portfolio
                if (e.ColumnIndex == 0)
                {
                    PortfolioService.Rename(oldValue.ToString(), newValue);

                    if (Convert.ToBoolean(grid[1, e.RowIndex].Value))
                    {
                        PortfolioService.SetStartup(newValue);
                    }
                }

                //Set startup
                if (e.ColumnIndex == 1)
                {
                    for (int i = 0; i < grid.Rows.Count; i++)
                    {
                        if (i == e.RowIndex)
                        {
                            PortfolioService.SetStartup(Convert.ToBoolean(newValue) ? grid[0, i].Value.ToString() : string.Empty);
                            continue;
                        }

                        grid[1, i].Value = false;
                    }
                }
            }

            //Cancelled new row
            else if (oldValue == null && string.IsNullOrEmpty(newValue))
            {
                grid.CancelEdit();
            }

            //Existing row empty name
            else if (oldValue != null && string.IsNullOrEmpty(newValue))
            {
                grid[0, e.RowIndex].Value = oldValue;
            }
        }
        public ActionResult <Portfolio> Create(Portfolio portfolio)
        {
            _portfolioService.Create(portfolio);

            return(CreatedAtRoute("GetPortfolio", new { id = portfolio.Id.ToString() }, portfolio));
        }