public async Task <PhoneBookEntry> AddPhoneBookEntryAsync(string name, string phoneNumber, int phoneBookId)
        {
            var phoneBook = await phoneBookService.GetPhoneBookByIdAsync(phoneBookId);

            if (phoneBook == null)
            {
                //should define custom exceptions
                throw new Exception("Phone book not found");
            }

            var phoneBookEntry = await GetPhoneBookEntryByNumberAsync(phoneNumber);

            //check if number already exists
            if (phoneBookEntry == null)
            {
                phoneBookEntry = new PhoneBookEntry
                {
                    Name        = name,
                    PhoneNumber = phoneNumber,
                    PhoneBookId = phoneBookId
                };

                await _phoneBookEntryRepository.AddAsync(phoneBookEntry);
            }

            await _phoneBookEntryRepository.SaveChangesAsync();

            // Logic on what to do if number already exists.
            //add custom response
            return(phoneBookEntry);
        }
Exemple #2
0
        public async Task <IActionResult> Get(int id)
        {
            try
            {
                var phoneBook = await phoneBookService.GetPhoneBookByIdAsync(id);

                if (phoneBook == null)
                {
                    return(new NotFoundResult());
                }

                return(new OkObjectResult(phoneBook));
            }
            catch (Exception ex)
            {
                return(new BadRequestObjectResult(ex.Message));
            }
        }