Example #1
0
        public async Task Test11()
        {
            var myUser = new MyUserForSave
            {
                Name              = "Ahmad Akra",
                Name2             = "أحمد عكره",
                PreferredLanguage = "en"
            };

            // Verify that the id was deleted by calling get
            var response = await Client.PostAsJsonAsync($"{Url}/me", myUser);

            Output.WriteLine(await response.Content.ReadAsStringAsync());
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);

            var responseData = await response.Content.ReadAsAsync <GetByIdResponse <User> >();

            // Assert that the response is correct
            Assert.Equal(nameof(User), responseData.CollectionName);
            Assert.Equal(myUser.Name, responseData.Result.Name);
            Assert.Equal(myUser.Name2, responseData.Result.Name2);
            Assert.Equal(myUser.Name3, responseData.Result.Name3);
            Assert.Equal(myUser.PreferredLanguage, responseData.Result.PreferredLanguage);

            Shared.Set("MyUser", myUser);
        }
Example #2
0
 public async Task <ActionResult <GetByIdResponse <User> > > SaveMyUser([FromBody] MyUserForSave me)
 {
     return(await ControllerUtilities.InvokeActionImpl(async() =>
     {
         GetByIdResponse <User> result = await SaveMyUserImpl(me);
         Response.Headers.Set("x-user-settings-version", Constants.Stale);
         return Ok(result);
     }, _logger));
 }
Example #3
0
 public async Task <ActionResult <GetByIdResponse <User> > > SaveMyUser([FromBody] MyUserForSave me)
 {
     return(await ControllerUtilities.InvokeActionImpl(async() =>
     {
         User user = await GetService().SaveMyUser(me);
         GetByIdResponse <User> result = TransformToResponse(user, cancellation: default);
         Response.Headers.Set("x-user-settings-version", Constants.Stale);
         return Ok(result);
     }, _logger));
 }
Example #4
0
        public async Task <ActionResult <GetByIdResponse <User> > > SaveMyUser([FromBody] MyUserForSave me)
        {
            User user = await _service.SaveMyUser(me);

            GetByIdResponse <User> result = TransformToResponse(user, cancellation: default);

            Response.Headers.Set("x-user-settings-version", Constants.Stale);

            return(Ok(result));
        }
Example #5
0
        private async Task <GetByIdResponse <User> > SaveMyUserImpl([FromBody] MyUserForSave me)
        {
            int myId = _appRepo.GetUserInfo().UserId.Value;
            var user = await _appRepo.Users.Expand("Roles").FilterByIds(myId).FirstOrDefaultAsync();

            // Create a user for save
            var userForSave = new UserForSave
            {
                Id                = user.Id,
                Email             = user.Email,
                Name              = me.Name?.Trim(),
                Name2             = me.Name2?.Trim(),
                Name3             = me.Name3?.Trim(),
                PreferredLanguage = me.PreferredLanguage?.Trim(),
                Image             = me.Image,
                EntityMetadata    = new EntityMetadata
                {
                    [nameof(UserForSave.Id)]                = FieldMetadata.Loaded,
                    [nameof(UserForSave.Email)]             = FieldMetadata.Loaded,
                    [nameof(UserForSave.Name)]              = FieldMetadata.Loaded,
                    [nameof(UserForSave.Name2)]             = FieldMetadata.Loaded,
                    [nameof(UserForSave.Name3)]             = FieldMetadata.Loaded,
                    [nameof(UserForSave.PreferredLanguage)] = FieldMetadata.Loaded,
                    [nameof(UserForSave.Image)]             = FieldMetadata.Loaded
                },

                // The roles must remain the way they are
                Roles = user.Roles?.Select(e => new RoleMembershipForSave
                {
                    Id             = e.Id,
                    Memo           = e.Memo,
                    RoleId         = e.RoleId,
                    UserId         = e.UserId,
                    EntityMetadata = new EntityMetadata
                    {
                        [nameof(RoleMembershipForSave.Id)]     = FieldMetadata.Loaded,
                        [nameof(RoleMembershipForSave.Memo)]   = FieldMetadata.Loaded,
                        [nameof(RoleMembershipForSave.RoleId)] = FieldMetadata.Loaded,
                        [nameof(RoleMembershipForSave.UserId)] = FieldMetadata.Loaded
                    },
                })
                        .ToList()
            };

            var entities = new List <UserForSave>()
            {
                userForSave
            };

            // Start a transaction scope for save since it causes data modifications
            using var trx = ControllerUtilities.CreateTransaction(null, GetSaveTransactionOptions());

            // Validation
            await SaveValidateAsync(entities);

            if (!ModelState.IsValid)
            {
                // TODO map the errors
                throw new UnprocessableEntityException(ModelState);
            }

            // Save and retrieve response
            await SaveExecuteAsync(entities, null, false);

            var response = await GetMyUserImpl();

            // Commit and return
            trx.Complete();
            return(response);
        }