Exemplo n.º 1
0
        public async Task <IActionResult> AddClient([FromBody] Client client)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    client.Id = Guid.NewGuid();

                    // At this point only regular clients can be created.
                    client.IdClientRole = 1;

                    var clientLogin = _loginHelper.CreateNewLogin(client.Id, client.Name);

                    _context.Client.Add(client);
                    _context.Login.Add(clientLogin);

                    await _context.SaveChangesAsync();

                    transaction.Commit();
                    return(Ok("Client succesfully added"));
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    return(HttpBadRequest("Could not create the client"));
                }
            }
        }