Ejemplo n.º 1
0
        public IActionResult OnPutGridRow(int key, string values)
        {
            try
            {
                var transaction = _context.BeginTransaction();

                var existingRecord = _context.PhoneBooks.FirstOrDefault(i => i.IdPb.Equals(key));
                var _values        = JsonConvert.DeserializeObject <DataModels.PhoneBook>(values);

                if (!TryValidateModel(existingRecord))
                {
                    return(BadRequest("Validation failed"));
                }

                var updated = _context.PhoneBooks.Where(i => i.IdPb.Equals(key))
                              .Set(w => w.IdPb, _values.IdPb == 0 ? existingRecord.IdPb : _values.IdPb)
                              .Set(w => w.Name, _values.Name ?? existingRecord.Name)
                              .Set(w => w.Patronymic, _values.Patronymic ?? existingRecord.Patronymic)
                              .Set(w => w.Phone, _values.Phone ?? existingRecord.Phone)
                              .Set(w => w.Surname, _values.Surname ?? existingRecord.Surname)
                              .Set(w => w.Sex, _values.Sex ?? existingRecord.Sex)
                              .Update();

                if (updated != 1)
                {
                    return(BadRequest("ќшибка при сохранении записи."));
                }
                else
                {
                    int idpbh       = 0;
                    var query       = from p in _context.PhoneBookHistories orderby p.IdPbh descending select p;
                    var queryresult = query.FirstOrDefault();
                    if (queryresult != null)
                    {
                        idpbh = queryresult.IdPbh;
                    }

                    _context.PhoneBookHistories
                    .Value(p => p.Date, DateTime.Now)
                    .Value(p => p.IdPb, _values.IdPb == 0 ? existingRecord.IdPb : _values.IdPb)
                    .Value(p => p.IdPbh, idpbh + 1)
                    .Value(p => p.Name, _values.Name ?? existingRecord.Name)
                    .Value(p => p.Patronymic, _values.Patronymic ?? existingRecord.Patronymic)
                    .Value(p => p.Phone, _values.Phone ?? existingRecord.Phone)
                    .Value(p => p.Surname, _values.Surname ?? existingRecord.Surname)
                    .Value(p => p.Sex, _values.Sex ?? existingRecord.Sex)
                    .Insert();
                }
                transaction.Commit();
                return(new OkResult());
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(ex.ToString()));
            }
        }
Ejemplo n.º 2
0
        public IActionResult OnPost(int id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            try
            {
                var transaction = _context.BeginTransaction();
                PBook.IdPb = id;
                var updated = _context.Update(PBook);

                if (updated != 1)
                {
                    return(BadRequest("ќшибка при сохранении записи."));
                }
                else
                {
                    int idpbh       = 0;
                    var query       = from p in _context.PhoneBookHistories orderby p.IdPbh descending select p;
                    var queryresult = query.FirstOrDefault();
                    if (queryresult != null)
                    {
                        idpbh = queryresult.IdPbh;
                    }

                    _context.PhoneBookHistories
                    .Value(p => p.Date, DateTime.Now)
                    .Value(p => p.IdPb, PBook.IdPb)
                    .Value(p => p.IdPbh, idpbh + 1)
                    .Value(p => p.Name, PBook.Name)
                    .Value(p => p.Patronymic, PBook.Patronymic)
                    .Value(p => p.Phone, PBook.Phone)
                    .Value(p => p.Surname, PBook.Surname)
                    .Value(p => p.Sex, PBook.Sex)
                    .Insert();
                }
                transaction.Commit();
                return(Redirect("/"));
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(ex.ToString()));
            }
        }