Exemplo n.º 1
0
 public GetPhoneBookResponses GetPhoneBooks()
 {
     try
     {
         using (var dbcontext = new PhoneBooksContext())
         {
             var phonebooks = dbcontext.PhoneBooks.ToList();
             GetPhoneBookResponses getPhoneBookResponses = new GetPhoneBookResponses();
             getPhoneBookResponses.Books = new List <BookResponses>();
             foreach (var book in phonebooks)
             {
                 getPhoneBookResponses.Books.Add(new BookResponses {
                     ID = book.Id, Name = book.Name
                 });
             }
             return(new GetPhoneBookResponses {
                 Books = getPhoneBookResponses.Books, Success = true, Message = string.Empty
             });
         }
     }
     catch (Exception ex)
     {
         return(new GetPhoneBookResponses {
             Books = null, Success = false, Message = ex.Message
         });
     }
 }
Exemplo n.º 2
0
        private List <EntryResponses> getEntriesByPhoneBookID(int phonebookID)
        {
            List <EntryResponses> entryResponses = new List <EntryResponses>();

            using (var dbcontext = new PhoneBooksContext())
            {
                var entries = dbcontext.PhoneBookEntries.Where(entry => entry.PhoneBooksId.Equals(phonebookID)).ToList();
                foreach (var entry in entries)
                {
                    EntryResponses newEntry = new EntryResponses
                    {
                        ID          = entry.Id,
                        Name        = entry.Name,
                        PhoneNumber = entry.PhoneNumber
                    };
                    entryResponses.Add(newEntry);
                }
                return(entryResponses);
            }
        }
Exemplo n.º 3
0
 public LoginResponses GetLoginPhoneBooks(GetLoginRequest getLoginRequest)
 {
     try
     {
         using (var dbcontext = new PhoneBooksContext())
         {
             var users = dbcontext.Logins.Where(user => user.Password.Equals(getLoginRequest.Password) &&
                                                user.Username.Equals(getLoginRequest.Username)).FirstOrDefault();
             return(new LoginResponses {
                 Username = users.Username, Success = true, Message = string.Empty
             });
         }
     }
     catch (Exception ex)
     {
         return(new LoginResponses {
             Username = string.Empty, Success = false, Message = ex.Message
         });
     }
 }
Exemplo n.º 4
0
 public PhoneBookResponses GetPhoneEntryByPhoneBookName(GetPhoneBookRequest getPhoneBook)
 {
     try
     {
         using (var dbcontext = new PhoneBooksContext())
         {
             var phonebooks = dbcontext.PhoneBooks.Where(p => p.Name.Equals(getPhoneBook.Name)).FirstOrDefault();
             var getEntries = this.getEntriesByPhoneBookID(phonebooks.Id);
             return(new PhoneBookResponses {
                 PhoneBookName = phonebooks.Name, GetEntryResponses = getEntries.ToList(), Success = true, Message = string.Empty
             });
         }
     }
     catch (Exception ex)
     {
         return(new PhoneBookResponses {
             PhoneBookName = string.Empty, GetEntryResponses = null, Success = false, Message = ex.Message
         });
     }
 }
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
         });
     }
 }