コード例 #1
0
        public async Task <object> AddEditPreference(PreferenceRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(JsonResults.Error(400, ModelState.Values.FirstOrDefault().Errors.FirstOrDefault().ErrorMessage.ToString()));
            }

            try
            {
                var checkPreference = await _preferenceService.CheckPreference(request);

                if (!checkPreference)
                {
                    var model = new Preference
                    {
                        UserId    = request.UserId,
                        ServiceId = request.ServiceId,
                        Checked   = true
                    };

                    await _preferenceService.AddPreference(model);

                    return(JsonResults.Success(GetPreferenceModel(model)));
                }

                var preference = await _preferenceService.GetPreference(request.Id);

                preference.ServiceId = request.ServiceId;
                preference.Checked   = true;

                await _preferenceService.UpdatePreference(preference);

                return(JsonResults.Success(GetPreferenceModel(preference)));
            }
            catch (Exception ex)
            {
                return(JsonResults.Error(400, ex.Message));
            }
        }