コード例 #1
0
        public ActionResult Info()
        {
            try
            {
                Stream req  = Request.Body;
                string json = new StreamReader(req).ReadToEnd();

                w2uiJsonFormPostModel <Group> model = null;
                try
                {
                    model = JsonConvert.DeserializeObject <w2uiJsonFormPostModel <Group> >(json);
                }

                catch (Exception ex)
                {
                    return(Json(w2uiBaseModel.Error(ex)));
                }

                if (model.cmd == "save-record")
                {
                    if (!TryValidateModel(model.record, nameof(Student)))
                    {
                        var errors = ModelState
                                     .Values
                                     .Select(el => el.Errors.Select(m => m.ErrorMessage).FirstOrDefault())
                                     .ToList();

                        var msg = string.Join(", ", errors);
                        return(Json(w2uiBaseModel.Error(msg)));
                    }
                    ;

                    var record = model.record;

                    var gr = _db.Groups.FirstOrDefault(el => el.Id == record.Id);
                    if (gr == null)
                    {
                        throw new Exception("Не найдена группа");
                    }

                    gr.Title = record.Title;

                    _db.SaveChanges();
                }

                return(Json(new w2uiBaseModel()));
            }
            catch (Exception ex)
            {
                return(Json(w2uiBaseModel.Error(ex)));
            }
        }
コード例 #2
0
        public ActionResult Info()
        {
            try
            {
                Stream req  = Request.Body;
                string json = new StreamReader(req).ReadToEnd();

                w2uiJsonFormPostModel <StudentAPI> model = null;
                try
                {
                    model = JsonConvert.DeserializeObject <w2uiJsonFormPostModel <StudentAPI> >(json);
                }

                catch (Exception ex)
                {
                    return(Json(w2uiBaseModel.Error(ex)));
                }

                if (model.cmd == "save-record")
                {
                    var record = model.record;

                    //чтобы не записывалась пустая строка
                    if (string.IsNullOrEmpty(record.CallSign))
                    {
                        record.CallSign = null;
                    }

                    if (!TryValidateModel(record, nameof(Student)))
                    {
                        var errors = ModelState
                                     .Values
                                     .Select(el => el.Errors.Select(m => m.ErrorMessage).FirstOrDefault())
                                     .ToList();

                        var msg = string.Join(", ", errors);
                        return(Json(w2uiBaseModel.Error(msg)));
                    }
                    ;


                    var st = _db.Students.FirstOrDefault(el => el.Id == record.Id);
                    if (st == null)
                    {
                        throw new Exception("Не найден student");
                    }

                    var oldGroupList = _db
                                       .UserInGroups
                                       .Where(el => el.UserId == st.Id)
                                       .Select(el => el.GroupId)
                                       .ToList();

                    var newGroupList = record.Groups.Select(el => el.id).ToList();

                    //на добавление
                    var forAddIds = newGroupList.Except(oldGroupList).ToList();
                    var forAdd    = forAddIds.Select(
                        el => new UserInGroup
                    {
                        GroupId = el,
                        UserId  = record.Id
                    })
                                    .ToList();

                    //на удаление
                    var forDelIds = oldGroupList.Except(newGroupList).ToList();
                    var forDel    = _db.UserInGroups.Where(el => forDelIds.Contains(el.GroupId));

                    st.FirstName  = record.FirstName;
                    st.GenderId   = record.GenderId;
                    st.LastName   = record.LastName;
                    st.SecondName = record.SecondName;
                    st.CallSign   = record.CallSign;

                    _db.SaveChanges();

                    _db.UserInGroups.AddRange(forAdd);
                    _db.UserInGroups.RemoveRange(forDel);

                    _db.SaveChanges();
                }

                return(Json(new w2uiBaseModel()));
            }
            catch (Exception ex)
            {
                return(Json(w2uiBaseModel.Error(ex)));
            }
        }