コード例 #1
0
        public async Task<IActionResult> PostProJectType(ProJectTypeModelView Model)
        {

            var user = await userManager.FindByIdAsync(User.FindFirst("Id")?.Value);
            if (!await userManager.IsInRoleAsync(user, "Adman") && user.Block) return Unauthorized("Only  Adman");


            var count = _context.ProJectType.Where(i => i.AName == Model.AName || i.Name == Model.Name).ToList().Count;
            if(count==0)
            {

                var ProJectType = new ProJectType();
                ProJectType.AName = Model.AName;
                ProJectType.Name = Model.Name;

                _context.ProJectType.Add(ProJectType);
                _context.SaveChanges();
                _context.ProForm.AddRange(Model.ProForm.Select(i => new ProForm()
                {

                    Name = i.Name,
                    AName = i.AName,
                    Type = i.Type,
                    Required = i.Required,
                    ProJectTypeId = ProJectType.id
                }));



                _context.SaveChanges();






                return Ok(new
                {

                    ProJectType.id,
                    ProJectType.Name,
                    ProJectType.AName,
                    ProForm = ProJectType.ProForm.Select(i => new { i.AName, i.Id, i.Name, i.Type, i.Required }).ToList(),
                });

            }
            else
            {
                return BadRequest("Name And AName is  unique value");
            }

        }
コード例 #2
0
        public async Task<IActionResult> PutProJectType(int id, ProJectTypeUpDateModelView Model)
        {

            try
            {
                var user = await userManager.FindByIdAsync(User.FindFirst("Id")?.Value);
                if (!await userManager.IsInRoleAsync(user, "Adman") && user.Block) return Unauthorized("Only  Adman");
                var ProJectType = new ProJectType();
                var forms = new List<ProForm>();
                var count = _context.ProJectType.Where(i => (i.AName == Model.AName || i.Name == Model.Name) && i.id != id).ToList().Count;
                if (count == 0)
                {
                     ProJectType = _context.ProJectType.SingleOrDefault(i => i.id == id);
                    if (ProJectType == null) return NotFound();
                    ProJectType.AName = Model.AName;
                    ProJectType.Name = Model.Name;
                   
                  
                    foreach (var item in Model.ProForm)
                    {

                        var form = _context. ProForm.SingleOrDefault(i => i.Id == item.Id && id == i.ProJectTypeId);
                      
                        if (form != null) {
                            form.Name = item.Name;
                            form.AName = item.AName;
                            form.Type = item.Type;
                            form.Required = item.Required;
                            forms.Add(form);
                        }
                        else
                        {
                            form = new ProForm { Name = item.Name, AName = item.AName, Type = item.Type, Required = item.Required, ProJectTypeId = id };
                            forms.Add(form);
                        }
                    }
                    var Delete=_context.ProForm.Where(i => i.ProJectTypeId == id).ToList();
                    _context.ProForm.RemoveRange(Delete);
                    _context.SaveChanges();
                    _context.ProForm.AddRange(forms);
                    _context.SaveChanges();
                    forms = _context.ProForm.Where(i => i.ProJectTypeId == id).ToList();


                }
                else throw new Exception("Name And AName is  unique value");

                return Ok(new
                {

                    ProJectType.id,
                    ProJectType.Name,
                    ProJectType.AName,
                    ProForm = forms.Select(i => new { i.AName, i.Id, i.Name, i.Type, i.Required }).ToList(),
                });
            }
            catch (Exception e) { return BadRequest(e.InnerException); }



        }