public async Task <IActionResult> PutPartialForm([FromRoute] Guid id, [FromBody] PartialForm partialForm)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != partialForm.PartialId)
            {
                return(BadRequest());
            }

            _context.Entry(partialForm).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PartialFormExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
예제 #2
0
        public IHttpActionResult PartialUpdate(string symbol, PartialForm form)
        {
            if (_stocks.All(n => n.Symbol != symbol))
            {
                return(NotFound());
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
예제 #3
0
        public async Task <PartialForm> GetFormAsync(string id)
        {
            _logger.LogInformation($"Getting form from API with name: {id}");
            PartialForm result = new PartialForm();

            using (HttpClient httpClient = new HttpClient())
            {
                try
                {
                    result = JsonConvert.DeserializeObject <PartialForm>(
                        await httpClient.GetStringAsync(_apiaddress + id)
                        );
                }
                catch (Exception Ex)
                {
                    _logger.LogError($"Failed to get form: {Ex.Message}");
                    return(result);
                }
                return(result);
            }
        }
        public async Task <IActionResult> PostPartialForm(/*[FromBody] PartialForm Json*/ [FromBody] object Json)
        {
            PartialForm partialForm = new PartialForm();

            List <Control> controls = new List <Control>();

            List <Event> events = new List <Event>();

            try
            {
                Console.Write(Json.ToString() + "testing");
                partialForm         = JsonConvert.DeserializeObject <PartialForm>(Json.ToString());
                partialForm.Control = FlattenHirachy(partialForm.Control.ToList(), null, partialForm.PartialId);
                events            = partialForm.Event.ToList();
                partialForm.Event = events;
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                //Update Add or Delete rules on Events


                //Update Add or Delete events on the form.
                foreach (var Event in partialForm.Event)
                {
                    foreach (var Rule in Event.Rule)
                    {
                        if (Rule.RuleId == Guid.Empty)
                        {
                            _context.Rule.Add(Rule);
                        }
                        else
                        {
                            _context.Rule.Update(Rule);
                        }
                    }
                    if (Event.EventId == Guid.Empty)
                    {
                        _context.Event.Add(Event);
                    }
                    else
                    {
                        _context.Event.Update(Event);
                    }
                }

                //Update Add or Delete controls ond the form based on the annotation.
                foreach (var Control in partialForm.Control)
                {
                    //if (Control.Event.Count < 1)
                    //{
                    if (Control.IsNew)
                    {
                        _context.Control.Add(Control);
                    }
                    else
                    {
                        _context.Control.Update(Control);
                    }
                    //}
                    //else
                    //{
                    //    foreach (var Event in Control.Event)
                    //    {
                    //        Event.Control = null;
                    //        Event.ControlId = Control.ControlId;

                    //    }
                    //    Control.Event.Clear();
                    //}
                }

                //Get list of contsols form DB

                var refrenceControls = _context.Control
                                       .Where(m => m.PartialId == partialForm.PartialId)
                                       .ToList();
                //Find controls that nolonger exist.
                var result = refrenceControls.Where(p => !partialForm.Control.Any(p2 => p2.ControlId == p.ControlId));

                // Delete the controls from the database.
                foreach (var c in result)
                {
                    c.ParentControl        = null;
                    c.ParentControlId      = null;
                    c.InverseParentControl = null;
                    c.PartialId            = null;
                    _context.Control.Update(c);
                    _context.Control.Remove(c);
                }

                //Update or Add the list if this is a new list.
                if (partialForm.PartialId == Guid.Empty)
                {
                    _context.PartialForm.Add(partialForm);
                }
                else
                {
                    _context.PartialForm.Update(partialForm);
                }
                await _context.SaveChangesAsync();
            }
            catch (Exception Ex)
            {
                Console.Write(Ex);
                return(StatusCode(StatusCodes.Status500InternalServerError, Ex.Message));
            }

            //Re-read the form to populate the GUIDs
            partialForm = _context.PartialForm
                          .Include(pf => pf.Control)
                          .SingleOrDefault(m => m.PartialId == partialForm.PartialId);

            //Sort controls into hierarachy again after FlattenHierarchy mangles them
            foreach (var item in partialForm.Control)
            {
                item.InverseParentControl = partialForm.Control.Where(x => x.ParentControlId.HasValue && x.ParentControlId == item.ControlId).OrderBy(x => x.Order).ToList();
            }
            partialForm.Control = partialForm.Control.Where(x => !x.ParentControlId.HasValue).OrderBy(x => x.Order).ToList();

            return(CreatedAtAction("GetPartialForm", new { id = partialForm.PartialId }, partialForm));
        }
        public async Task <IActionResult> PostPartialForm(/*[FromBody] PartialForm Json*/ [FromBody] object Json)
        {
            PartialForm partialForm = new PartialForm();

            try
            {
                partialForm         = JsonConvert.DeserializeObject <PartialForm>(Json.ToString());
                partialForm.Control = FlattenHirachy(partialForm.Control.ToList(), null, partialForm.PartialId);
            } catch (Exception ex)
            {
                Console.Write(ex);
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                //Update Add or Delete controls ond the form based on the annotation.
                foreach (var Control in partialForm.Control)
                {
                    if (Control.IsNew)
                    {
                        _context.Control.Add(Control);
                    }
                    else
                    {
                        _context.Control.Update(Control);
                    }
                }

                //Get list of contsols form DB

                var refrenceControls = _context.Control
                                       .Where(m => m.PartialId == partialForm.PartialId)
                                       .ToList();
                //Find controls that nolonger exist.
                var result = refrenceControls.Where(p => !partialForm.Control.Any(p2 => p2.ControlId == p.ControlId));

                // Delete the controls from the database.
                foreach (var c in result)
                {
                    c.ParentControl        = null;
                    c.ParentControlId      = null;
                    c.InverseParentControl = null;
                    c.PartialId            = null;
                    _context.Control.Update(c);
                    _context.Control.Remove(c);
                }

                //Update or Add the list if this is a new list.
                if (partialForm.PartialId == Guid.Empty)
                {
                    _context.PartialForm.Add(partialForm);
                }
                else
                {
                    _context.PartialForm.Update(partialForm);
                }
                await _context.SaveChangesAsync();
            }
            catch (Exception Ex)
            {
                Console.Write(Ex);
            }


            return(CreatedAtAction("GetPartialForm", new { id = partialForm.PartialId }, partialForm));
        }