public async Task <IActionResult> Edit(int id, [Bind("ID,Name,PhoneNumber,EmailAddress")] PhoneBooks phoneBooks)
        {
            if (id != phoneBooks.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(phoneBooks);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PhoneBooksExists(phoneBooks.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(phoneBooks));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> PutPhoneBook([FromRoute] int id, [FromBody] PhoneBooks phoneBook)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != phoneBook.ID)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
        public async Task <IActionResult> Create([Bind("ID,Name,PhoneNumber,EmailAddress")] PhoneBooks phoneBooks)
        {
            if (ModelState.IsValid)
            {
                _context.Add(phoneBooks);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(phoneBooks));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> PostPhoneBook([FromBody] PhoneBooks phoneBook)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.PhoneBook.Add(phoneBook);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetPhoneBook", new { id = phoneBook.ID }, phoneBook));
        }
Exemplo n.º 5
0
 public CreatePhoneBookResponses CreatePhoneBooks(CreatePhoneBookRequest createPhoneBookRequest)
 {
     try
     {
         using (var dbcontext = new PhoneBooksContext())
         {
             PhoneBooks addBook = new PhoneBooks {
                 Name = createPhoneBookRequest.PhoneBookName
             };
             var checkPhoneBookNameIFExists = dbcontext.PhoneBooks.Where(name => name.Name.Equals(createPhoneBookRequest.PhoneBookName)).SingleOrDefault();
             int phonebookID;
             if (checkPhoneBookNameIFExists.Id < 0)
             {
                 dbcontext.PhoneBooks.Add(addBook);
                 dbcontext.SaveChanges();
                 phonebookID = addBook.Id;
             }
             else
             {
                 phonebookID = checkPhoneBookNameIFExists.Id;
             }
             PhoneBookEntry bookEntry = new PhoneBookEntry
             {
                 Name         = createPhoneBookRequest.Name,
                 PhoneBooksId = phonebookID,
                 PhoneNumber  = createPhoneBookRequest.PhoneNumber
             };
             dbcontext.PhoneBookEntries.Add(bookEntry);
             dbcontext.SaveChanges();
             return(new CreatePhoneBookResponses {
                 Success = true, Message = string.Empty
             });
         }
     }
     catch (Exception ex)
     {
         return(new CreatePhoneBookResponses {
             Success = false, Message = ex.Message
         });
     }
 }
Exemplo n.º 6
0
        public MainWindow(string x, string y)
        {
            InitializeComponent();
            this.Current_Owner.Text = x + " " + y;
            ActualOwner.fName       = x;
            ActualOwner.lName       = y;


            DataContext = new Start_model();


            using (OwnerEntities db = new OwnerEntities())
            {
                ActualOwner.ID = db.Owners.Where(o => o.fName == x).Where(o => o.lName == y).FirstOrDefault().ID;


                if (db.Countries.Count() == 0)
                {
                    foreach (var item in Country.GetValues(typeof(Country)))
                    {
                        Countries c = new Countries()
                        {
                            Country = item.ToString()
                        };
                        db.Countries.Add(c);
                    }
                    db.SaveChanges();
                } // wypełnienie bazy Krajami z enum

                if (db.PhoneBooks.Where(a => a.OwnerID == db.Owners.Where(o => o.fName == ActualOwner.fName).Where(o => o.lName == ActualOwner.lName).FirstOrDefault().ID).Count() == 0)
                {
                    PhoneBooks book = new PhoneBooks()
                    {
                        OwnerID = db.Owners.Where(o => o.fName == ActualOwner.fName).Where(o => o.lName == ActualOwner.lName).First().ID
                    };
                    db.PhoneBooks.Add(book);
                    db.SaveChanges();
                }  //stworzenie książki dla aktualnego usera, jeżeli nie istnieje
            }
        }