Ejemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] ClientViewModel vm)
        {
            if (vm == null)
            {
                return(await ResponseProvider.ParameterErrorResponse("Request parameter is null"));
            }

            if (string.IsNullOrEmpty(vm.ClientName))
            {
                return(await ResponseProvider.ParameterErrorResponse($"Parameter's {nameof(vm.ClientName)} is null or empty"));
            }

            //checke client name
            if (await _DbContext.Clients.AnyAsync(m => m.ClientName == vm.ClientName))
            {
                return(await ResponseProvider.ResourceExistResponse($"Parameter's {nameof(vm.ClientName)} is exist"));
            }

            var client = new Client()
            {
                ClientId           = Guid.NewGuid().ToString(),
                ClientName         = vm.ClientName,
                Enabled            = vm.Enabled,
                AllowOfflineAccess = vm.AllowOfflineAccess,
                Created            = DateTime.Now
            };

            var result = await _DbContext.AddAsync(client);

            await _DbContext.SaveChangesAsync();

            //update vm data
            vm.ClientId = result.Entity.ClientId;
            vm.Id       = result.Entity.Id;

            return(await ResponseProvider.OkResult(vm, "create success"));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Put([FromBody] ClientViewModel vm)
        {
            if (vm == null)
            {
                return(await ResponseProvider.ParameterErrorResponse("Request parameter is null"));
            }

            if (string.IsNullOrEmpty(vm.ClientName))
            {
                return(await ResponseProvider.ParameterErrorResponse($"Parameter's {nameof(vm.ClientName)} is null or empty"));
            }

            //checke client name
            if (await _DbContext.Clients.AnyAsync(m => m.ClientName == vm.ClientName && m.Id != vm.Id))
            {
                return(await ResponseProvider.ResourceExistResponse($"Parameter's {nameof(vm.ClientName)} is exist"));
            }

            var client = await _DbContext.Clients.FirstOrDefaultAsync(m => m.Id == vm.Id);

            //resouce is not found
            if (client == null)
            {
                return(await ResponseProvider.ResourceNotFoundResponse());
            }

            //update property
            client.ClientName         = vm.ClientName;
            client.AllowOfflineAccess = vm.AllowOfflineAccess;
            client.Enabled            = vm.Enabled;
            client.Updated            = DateTime.Now;

            var result = _DbContext.Update(client);
            await _DbContext.SaveChangesAsync();

            return(await ResponseProvider.OkResult(vm, "update success"));
        }