public async Task <IActionResult> Create(Reservation resIn) { Reservation res = await _services.Create(resIn); if (res != null) { return(CreatedAtRoute("GetReservationById", new { Id = res.Id }, res)); } else { return(BadRequest()); } }
public async Task <HttpResponseMessage> ReservationVideo(int videoId) { if (!User.Identity.IsAuthenticated) { return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "还未授权")); } var openId = User.Identity.Name; try { var video = _videoServices.GetById(videoId); if (video == null) { return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "不存在此视频!")); } var vm = new Reservation { VideoId = videoId, OpenId = openId.ToString(), CreatedDate = DateTime.Now }; var message = new SingleSendMessagesVM { ToUser = openId.ToString(), MsgType = "text", Text = new Message { Content = $"你已成功预约了直播:【{video.Title}】" } }; var returnMsg = "预约成功"; var res = _reservationServices.GetById(vm.VideoId, vm.OpenId); if (res != null) { _reservationServices.Delete(res); message.Text = new Message { Content = $"你已成功取消了直播:【{video.Title}】的预约" }; returnMsg = "已取消预约"; } else { _reservationServices.Create(vm); } var json = JsonConvert.SerializeObject(message, Newtonsoft.Json.Formatting.Indented, new JsonSerializerSettings { ContractResolver = new LowercaseContractResolver() }); HttpContent content = new StringContent(json); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); string accesstoken; if (_cacheServices.IsSet("access_token")) { accesstoken = (string)_cacheServices.Get("access_token"); } else { var atvm = await WeChatHepler.GetAccessTokenAsync(SettingsManager.WeiXin.AppId, SettingsManager.WeiXin.AppSecret); accesstoken = atvm.access_token; _cacheServices.Set("access_token", accesstoken, 120); } var result = await WeChatHepler.SingleSendMessagesAsync(accesstoken, content); _logger.Info($"单发结果[errcode:{result.ErrCode},errmsg:{result.ErrMsg}];消息主体:{json}"); return(Request.CreateResponse(HttpStatusCode.OK, returnMsg)); } catch (Exception ex) { _logger.Fatal(ex); return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message, ex)); } }