Exemplo n.º 1
0
 public async Task <string> AddUser(TandemUser newUser)
 {
     _logger.LogInformation(traceSearchString + "about to add user to cosmos with emailAddress: " + newUser.EmailAddress);
     try
     {
         using (var context = new TandemUserContext(_dbOptions))
         {
             context.Database.EnsureCreated();
             newUser.UserId = Guid.NewGuid();
             newUser.id     = "TandemUser|" + newUser.UserId.ToString();
             context.Add(newUser);
             await context.SaveChangesAsync();
         }
     }
     catch (CosmosException cosmosException)
     {
         if (cosmosException.StatusCode == HttpStatusCode.Conflict)
         {
             // TODO: would make this a constant
             return("duplicateEmail");
         }
         throw;
     }
     _logger.LogInformation(traceSearchString + "add user to cosmos with Id: " + newUser?.UserId);
     return(newUser.UserId.ToString());
 }
Exemplo n.º 2
0
 public TandemUserDto(TandemUser tandemUser)
 {
     if (tandemUser == null)
     {
         return;
     }
     UserId       = tandemUser.UserId;
     EmailAddress = tandemUser.EmailAddress;
     Name         = ((tandemUser.FirstName?.Trim() + " " + tandemUser.MiddleName?.Trim()).Trim() + " " + tandemUser.LastName?.Trim()).Trim();
     PhoneNumber  = tandemUser.PhoneNumber;
 }
Exemplo n.º 3
0
        public async Task <Guid> CreateUser(TandemUser user)
        {
            if (await GetUser(user.emailAddress) == null)
            {
                user.id = Guid.NewGuid();
                var container = GetContainer();
                await container.CreateItemAsync(user);

                return(user.id);
            }
            throw new Exception($"User with Email Address : {user.emailAddress} already exists");
        }
Exemplo n.º 4
0
        public async Task When_Adding_Existing_Email_User_Expect_Conflict()
        {
            // TODO: Instead of using hard-coded email address here, I would use same email from valid create test above AND make sure they run synchronously!!
            var newUser = new TandemUser()
            {
                EmailAddress = "*****@*****.**", FirstName = "Fred", LastName = "Flinstone", PhoneNumber = "654-987-1234"
            };
            var content  = new StringContent(newUser.ToString(), null, "application/json");
            var response = await _httpClient.PostAsync("api/v1/user/", content);

            Assert.True(response.StatusCode == HttpStatusCode.Conflict);
        }
Exemplo n.º 5
0
        public async Task When_Adding_Valid_User_Expect_Created()
        {
            // TODO: Instead of using Guid email address for uniqueness, I would use the same email address but delete it in test cleanup
            var newUser = new TandemUser()
            {
                EmailAddress = new Guid().ToString() + "@.flinstone.com", FirstName = "Fred", LastName = "Flinstone", PhoneNumber = "654-987-1234"
            };
            var content  = new StringContent(newUser.ToString(), null, "application/json");
            var response = await _httpClient.PostAsync("api/v1/user/", content);

            Assert.True(response.StatusCode == HttpStatusCode.Created);
        }
Exemplo n.º 6
0
        public async Task <IActionResult> Post([FromBody] TandemUser newUser)
        {
            _logger.LogInformation(traceSearchString + "about to add new user with following payload: ");
            var addUserResponse = await _userService.AddUser(newUser);

            // TODO: like in service, this response would be from a constant
            if (addUserResponse == "duplicateEmail")
            {
                return(Conflict());
            }
            _logger.LogInformation(traceSearchString + "added new user with userId: " + newUser.UserId);
            return(CreatedAtRoute("GetUserByEmailAddress", new { emailAddress = newUser.EmailAddress }, new TandemUserDto(newUser)));
        }
Exemplo n.º 7
0
        public static async Task Main(string[] args)
        {
            Console.WriteLine("Start the Tandem User Service Integration Tests!");
            Console.WriteLine("Please ensure that the User Service API is running first! \n");

            Console.WriteLine("Attempt a Health Check for the Data Store.");
            Console.WriteLine("Expect a 200 Success with an info message.");
            await TestGetUri("https://*****:*****@do-not-add.com");

            Console.ReadLine();

            Console.WriteLine("Attempt to create a new user with email [email protected].");
            Console.WriteLine("Expect a 201 Success with a new user.");
            var user = new TandemUser
            {
                EmailAddress = "*****@*****.**",
                FirstName    = "Jesse",
                LastName     = "Booth",
                MiddleName   = "Evan",
                PhoneNumber  = "256-797-6092"
            };

            await TestPostUri("https://*****:*****@test.com).");
            Console.WriteLine("Expect a 200 Success with the existing user(s).");
            await TestGetUri("https://*****:*****@test.com");

            Console.ReadLine();
        }
Exemplo n.º 8
0
        public async Task <IActionResult> Create([FromBody] TandemUser user)
        {
            try
            {
                var cosmosService = new CosmosService(Configuration);
                var userId        = await cosmosService.CreateUser(user);

                return(Ok($"User successfully created with id {userId}"));
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(StatusCode(StatusCodes.Status500InternalServerError, e.GetFullMessage()));
            }
        }
Exemplo n.º 9
0
        public static async Task TestPostUri(string uri, TandemUser user)
        {
            try
            {
                var content  = new StringContent(user.ToString(), Encoding.UTF8, "application/json");
                var response = await _client.PostAsync(uri, content).ConfigureAwait(false);

                response.EnsureSuccessStatusCode();
                var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                Console.WriteLine(responseBody);
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine("\nException Caught!");
                Console.WriteLine("\nMessage: {0} ", e.Message);
            }
        }
Exemplo n.º 10
0
 private async Task <TandemUser> AddUserToContainerAsync(TandemUser user)
 {
     user.EnsureId();
     return(await _container.CreateItemAsync(user, new PartitionKey(user.EmailAddress)).ConfigureAwait(false));
 }
Exemplo n.º 11
0
 public async Task <ActionResult <TandemUser> > CreateUser([FromBody] TandemUser user)
 {
     return(CreatedAtAction(nameof(CreateUser), await AddUserToContainerAsync(user).ConfigureAwait(false)));
 }