public IList <string> GetAllStates()
 {
     using (var pubs = new PubsEntities())
     {
         return(pubs.Authors.Select(a => a.State).Distinct().ToArray());
     }
 }
        public ActionResult EditAuthor(string id)
        {
            // 当該著者 ID のデータを読み取る
            Author editAuthor = null;

            using (var pubs = new PubsEntities())
            {
                editAuthor = pubs.Authors.FirstOrDefault(a => a.AuthorId == id);
            }

            // View に引き渡すデータを準備する
            var vm = new AuthorEditViewModel()
            {
                AuthorId        = editAuthor.AuthorId,
                AuthorFirstName = editAuthor.AuthorFirstName,
                AuthorLastName  = editAuthor.AuthorLastName,
                Phone           = editAuthor.Phone,
                State           = editAuthor.State
            };

            // View にデータを引き渡すにあたり、入力データと周辺データを分けておく。
            // (ViewModel に周辺データを入れることで、ViewModel を完全にフォームモデルに一致させるように設計)
            using (var pubs = new PubsEntities())
            {
                var query = pubs.Authors.Select(a => a.State).Distinct();
                ViewData["AllStates"] = query.ToList();
            }

            return(View(vm));
        }
        public ActionResult EditAuthor(string id)
        {
            // 当該著者 ID のデータを読み取る
            Author editAuthor = null;
            using (var pubs = new PubsEntities())
            {
                editAuthor = pubs.Authors.FirstOrDefault(a => a.AuthorId == id);
            }

            // View に引き渡すデータを準備する
            var vm = new AuthorEditViewModel()
            {
                AuthorId = editAuthor.AuthorId,
                AuthorFirstName = editAuthor.AuthorFirstName,
                AuthorLastName = editAuthor.AuthorLastName,
                Phone = editAuthor.Phone,
                State = editAuthor.State
            };

            // View にデータを引き渡すにあたり、入力データと周辺データを分けておく。
            // (ViewModel に周辺データを入れることで、ViewModel を完全にフォームモデルに一致させるように設計)
            using (var pubs = new PubsEntities())
            {
                var query = pubs.Authors.Select(a => a.State).Distinct();
                ViewData["AllStates"] = query.ToList();
            }

            return View(vm);
        }
        public ActionResult EditAuthor(string id, AuthorEditViewModel model)
        {
            using (var pubs = new PubsEntities())
            {
                var query = pubs.Authors.Select(a => a.State).Distinct();
                ViewData["AllStates"] = query.ToList();
            }

            // 送信されてきたデータを再チェック
            if (ModelState.IsValid == false)
            {
                // 前画面を返す
                // ID フィールドがロストしているので補完する
                model.AuthorId = id;
                return(View(model));
            }

            model.AuthorId = id;

            // データベースに登録を試みる
            using (var pubs = new PubsEntities())
            {
                var target = pubs.Authors.Where(a => a.AuthorId == model.AuthorId).FirstOrDefault();
                target.AuthorFirstName = model.AuthorFirstName;
                target.AuthorLastName  = model.AuthorLastName;
                target.Phone           = model.Phone;
                target.State           = model.State;

                pubs.SaveChanges();
            }
            // 一覧画面に帰る
            return(RedirectToAction("ListAuthors"));
        }
        public ActionResult EditAuthor(string id, AuthorEditViewModel model)
        {
            using (var pubs = new PubsEntities())
            {
                var query = pubs.Authors.Select(a => a.State).Distinct();
                ViewData["AllStates"] = query.ToList();
            }

            // 送信されてきたデータを再チェック
            if (ModelState.IsValid == false)
            {
                // 前画面を返す
                // ID フィールドがロストしているので補完する
                model.AuthorId = id;
                return View(model);
            }

            model.AuthorId = id;

            // データベースに登録を試みる
            using (var pubs = new PubsEntities())
            {
                var target = pubs.Authors.Where(a => a.AuthorId == model.AuthorId).FirstOrDefault();
                target.AuthorFirstName = model.AuthorFirstName;
                target.AuthorLastName = model.AuthorLastName;
                target.Phone = model.Phone;
                target.State = model.State;

                pubs.SaveChanges();
            }
            // 一覧画面に帰る
            return RedirectToAction("ListAuthors");
        }
 public string[] GetStates()
 {
     using (var pubs = new PubsEntities())
     {
         var query = pubs.Authors.Select(a => a.State).Distinct();
         return query.ToArray();
     }
 }
 public string[] GetStates()
 {
     using (var pubs = new PubsEntities())
     {
         var query = pubs.Authors.Select(a => a.State).Distinct();
         return(query.ToArray());
     }
 }
 public IActionResult Index()
 {
     var vm = new AuthorListViewModel();
     using(var pubs = new PubsEntities())
     {
         vm.Authors = pubs.Authors.ToArray();
     }
     return View(vm);
 }
Example #9
0
        public IActionResult Index()
        {
            var vm = new AuthorListViewModel();

            using (var pubs = new PubsEntities())
            {
                vm.Authors = pubs.Authors.ToArray();
            }
            return(View(vm));
        }
        public Author GetAuthorByAuthorId(string authorId)
        {
            if (Regex.IsMatch(authorId, @"^[0-9]{3}-[0-9]{2}-[0-9]{4}$") == false)
            {
                throw new ArgumentOutOfRangeException(nameof(authorId));
            }

            // 当該著者 ID のデータを読み取る
            using (var pubs = new PubsEntities())
            {
                return(pubs.Authors.FirstOrDefault(a => a.AuthorId == authorId));
            }
        }
 public IList <AuthorOverview> GetAuthors()
 {
     using (var pubs = new PubsEntities())
     {
         var query = pubs.Authors.Select(a => new AuthorOverview
         {
             AuthorId   = a.AuthorId,
             AuthorName = a.AuthorFirstName + " " + a.AuthorLastName,
             Phone      = a.Phone,
             State      = a.State,
             Contract   = a.Contract
         });
         return(query.ToArray());
     }
 }
 public IList<AuthorOverview> GetAllAuthors()
 {
     using (var pubs = new PubsEntities())
     {
         return pubs.Authors
             .Select(a => new AuthorOverview
             {
                 AuthorId = a.AuthorId,
                 AuthorName = a.AuthorFirstName + " " + a.AuthorLastName,
                 Phone = a.Phone,
                 State = a.State,
                 Contract = a.Contract
             })
             .ToArray();
     }
 }
        public void UpdateAuthor(UpdateAuthorRequest request)
        {
            if (ModelState.IsValid == false)
            {
                throw new ArgumentException();
            }

            // データベースに登録する
            using (var pubs = new PubsEntities())
            {
                var target = pubs.Authors.FirstOrDefault(a => a.AuthorId == request.AuthorId);
                target.AuthorFirstName = request.AuthorFirstName;
                target.AuthorLastName  = request.AuthorLastName;
                target.Phone           = request.Phone;
                target.State           = request.State;
                pubs.SaveChanges();
            }
        }
        public IList<AuthorOverview> GetAuthorsByState(string state)
        {
            if (Regex.IsMatch(state, "^[A-Z]{2}$") == false) throw new ArgumentOutOfRangeException(nameof(state));

            using (var pubs = new PubsEntities())
            {
                var query = pubs.Authors.Where(a => a.State == state)
                            .Select(a => new AuthorOverview()
                            {
                                AuthorId = a.AuthorId,
                                AuthorName = a.AuthorFirstName + " " + a.AuthorLastName,
                                Phone = a.Phone,
                                State = a.State,
                                Contract = a.Contract
                            });
                return query.ToArray();
            }
        }
 public ActionResult ListAuthors()
 {
     using (var pubs = new PubsEntities())
     {
         var query = pubs.Authors
                     .Select(a => new AuthorOverview
         {
             AuthorId   = a.AuthorId,
             AuthorName = a.AuthorFirstName + " " + a.AuthorLastName,
             Phone      = a.Phone,
             State      = a.State,
             Contract   = a.Contract
         });
         var vm = new AuthorOverviewListViewModel
         {
             Authors = query.ToArray()
         };
         return(View(vm));
     }
 }
        public IList <AuthorOverview> GetAuthorsByState(string state)
        {
            if (Regex.IsMatch(state, "^[A-Z]{2}$") == false)
            {
                throw new ArgumentOutOfRangeException(nameof(state));
            }

            using (var pubs = new PubsEntities())
            {
                var query = pubs.Authors.Where(a => a.State == state)
                            .Select(a => new AuthorOverview()
                {
                    AuthorId   = a.AuthorId,
                    AuthorName = a.AuthorFirstName + " " + a.AuthorLastName,
                    Phone      = a.Phone,
                    State      = a.State,
                    Contract   = a.Contract
                });
                return(query.ToArray());
            }
        }
 public ActionResult ListAuthors()
 {
     using (var pubs = new PubsEntities())
     {
         var query = pubs.Authors
                     .Select(a => new AuthorOverview
                     {
                         AuthorId = a.AuthorId,
                         AuthorName = a.AuthorFirstName + " " + a.AuthorLastName,
                         Phone = a.Phone,
                         State = a.State,
                         Contract = a.Contract
                     });
         var vm = new AuthorOverviewListViewModel
         {
             Authors = query.ToArray()
         };
         return View(vm);
     }
 }