public string Add(SBU sbu) { MyResponse response = new MyResponse(); try { SBU _sbu = db.SBU.FirstOrDefault(X => X.Name == sbu.Name); if (_sbu != null) { throw new Exception(StatusCode.ObjectHadExist.ToString()); } db.SBU.Add(sbu); db.SaveChanges(); } catch (Exception ex) { if (ex.Message == StatusCode.ObjectHadExist.ToString()) { response.Code = StatusCode.ObjectHadExist; } else { response.Code = StatusCode.Error; } } return(response.ToString()); }
public HttpResponseMessage GetList() { MyResponse response = new MyResponse(); try { List <SBU> sbuList = db.SBU.ToList(); string json = JsonConvert.SerializeObject(sbuList); response.Count = sbuList.Count.ToString(); response.Data = json; } catch (Exception ex) { if (ex.Message == StatusCode.ObjectNotFound.ToString()) { response.Code = StatusCode.ObjectNotFound; } else { response.Code = StatusCode.Error; } } return(new HttpResponseMessage { Content = new StringContent(response.ToString(), System.Text.Encoding.UTF8, "application/json") }); }
public HttpResponseMessage GetOne(string id) { MyResponse response = new MyResponse(); try { SBU sbu = db.SBU.Find(id); if (sbu == null) { throw new Exception(StatusCode.ObjectNotFound.ToString()); } string json = JsonConvert.SerializeObject(sbu); response.Data = json; } catch (Exception ex) { if (ex.Message == StatusCode.ObjectNotFound.ToString()) { response.Code = StatusCode.ObjectNotFound; } else { response.Code = StatusCode.Error; } } return(new HttpResponseMessage { Content = new StringContent(response.ToString(), System.Text.Encoding.UTF8, "application/json") }); }
public string Update(SBU sbu) { MyResponse response = new MyResponse(); try { //判断要改的新名字是否存在 SBU _sbu = db.SBU.Find(sbu.Name); if (_sbu == null) { throw new Exception(StatusCode.ObjectNotFound.ToString()); } db.Entry(sbu).State = EntityState.Modified; db.SaveChanges(); } catch (Exception ex) { if (ex.Message == StatusCode.ObjectNotFound.ToString()) { response.Code = StatusCode.ObjectNotFound; } else { response.Code = StatusCode.Error; } } return(response.ToString()); }
public string Delete(string id) { MyResponse response = new MyResponse(); try { SBU sbu = db.SBU.Find(id); if (sbu == null) { throw new Exception(StatusCode.ObjectNotFound.ToString()); } db.SBU.Remove(sbu); db.SaveChanges(); } catch (Exception ex) { if (ex.Message == StatusCode.ObjectNotFound.ToString()) { response.Code = StatusCode.ObjectNotFound; } else { response.Code = StatusCode.Error; } } return(response.ToString()); }
public string Delete(string code) { MyResponse response = new MyResponse(); try { User user = db.User.Find(code); if (user == null) { throw new Exception(StatusCode.ObjectNotFound.ToString()); } db.User.Remove(user); db.SaveChanges(); } catch (Exception ex) { if (ex.Message == StatusCode.ObjectNotFound.ToString()) { response.Code = StatusCode.ObjectNotFound; } else { response.Code = StatusCode.Error; } } return(response.ToString()); }
public string GetOne(int id) { MyResponse response = new MyResponse(); try { News news = db.News.Find(id); if (news == null) { throw new Exception(StatusCode.ObjectNotFound.ToString()); } string json = JsonConvert.SerializeObject(news); response.Data = json; } catch (Exception ex) { if (ex.Message == StatusCode.ObjectNotFound.ToString()) { response.Code = StatusCode.ObjectNotFound; } else { response.Code = StatusCode.Error; } } return(response.ToString()); }
public HttpResponseMessage GetOne(string token) { MyResponse response = new MyResponse(); try { AmbToken t = db.AmbToken.FirstOrDefault <AmbToken>(X => X.Token == token); if (t == null) { throw new Exception(StatusCode.ObjectNotFound.ToString()); } User u = db.User.FirstOrDefault <User>(X => X.UserCode == t.Code); if (u == null) { throw new Exception(StatusCode.ObjectNotFound.ToString()); } string json = JsonConvert.SerializeObject(u); response.Data = json; } catch (Exception ex) { if (ex.Message == StatusCode.ObjectNotFound.ToString()) { response.Code = StatusCode.ObjectNotFound; } else { response.Code = StatusCode.Error; } } return(new HttpResponseMessage { Content = new StringContent(response.ToString(), System.Text.Encoding.UTF8, "application/json") }); }
public HttpResponseMessage Login(string code, string pwd) { MyResponse response = new MyResponse(); try { if (string.IsNullOrEmpty(code) || string.IsNullOrEmpty(pwd)) { throw new Exception(StatusCode.ArgsNull.ToString()); } User user = db.User.FirstOrDefault <User>(X => X.UserCode == code & X.Password == pwd); if (user == null) { throw new Exception(StatusCode.ObjectNotFound.ToString()); } string token = CommonService.StrToMD5(code + pwd); //验证通过返回token,并保存到数据库 AmbToken oldToken = db.AmbToken.FirstOrDefault <AmbToken>(X => X.Token == token); if (oldToken != null) { db.AmbToken.Remove(oldToken); db.SaveChanges(); } AmbToken m = new AmbToken(); m.Token = token; m.Code = code; m.ExpireTime = DateTime.Now.AddMonths(1); db.AmbToken.Add(m); db.SaveChanges(); string json = JsonConvert.SerializeObject(token); response.Data = json; } catch (Exception ex) { if (ex.Message == StatusCode.ArgsNull.ToString()) { response.Code = StatusCode.ArgsNull; } else if (ex.Message == StatusCode.ObjectNotFound.ToString()) { response.Code = StatusCode.ObjectNotFound; } else { response.Code = StatusCode.Error; } } return(new HttpResponseMessage { Content = new StringContent(response.ToString(), System.Text.Encoding.UTF8, "application/json") }); }
public string Update(User user) { MyResponse response = new MyResponse(); try { HttpRequestMessage rm = new HttpRequestMessage(); db.Entry(user).State = EntityState.Modified; db.SaveChanges(); } catch { response.Code = StatusCode.Error; } return(response.ToString()); }
public string Register(string code, string pwd, string mobile) { MyResponse response = new MyResponse(); try { if (string.IsNullOrEmpty(code) || string.IsNullOrEmpty(pwd) || string.IsNullOrEmpty(mobile)) { throw new Exception(StatusCode.ArgsNull.ToString()); } User user = new User() { UserCode = code, Mobile = mobile, Password = pwd }; User u = db.User.FirstOrDefault <User>(X => X.UserCode == user.UserCode); if (u != null) { throw new Exception(StatusCode.ObjectHadExist.ToString()); } db.User.Add(user); db.SaveChanges(); } catch (Exception ex) { if (ex.Message == StatusCode.ArgsNull.ToString()) { response.Code = StatusCode.ArgsNull; } else if (ex.Message == StatusCode.ObjectHadExist.ToString()) { response.Code = StatusCode.ObjectHadExist; } else { response.Code = StatusCode.Error; } } return(response.ToString()); }
public HttpResponseMessage GetList() { MyResponse response = new MyResponse(); try { List <Photo> mList = db.Photo.ToList(); string json = JsonConvert.SerializeObject(mList); response.Data = json; response.Count = mList.Count.ToString(); } catch { response.Code = StatusCode.Error; } return(new HttpResponseMessage { Content = new StringContent(response.ToString(), System.Text.Encoding.UTF8, "application/json") }); }
public string Add(Schedule schedule) { MyResponse response = new MyResponse(); try { db.Schedules.Add(schedule); db.SaveChanges(); } catch (Exception ex) { if (ex.Message == StatusCode.ObjectNotFound.ToString()) { response.Code = StatusCode.ObjectNotFound; } else { response.Code = StatusCode.Error; } } return(response.ToString()); }
public string Update(Schedule schedule) { MyResponse response = new MyResponse(); try { db.Entry(schedule).State = EntityState.Modified; db.SaveChanges(); } catch (Exception ex) { if (ex.Message == StatusCode.ObjectNotFound.ToString()) { response.Code = StatusCode.ObjectNotFound; } else { response.Code = StatusCode.Error; } } return(response.ToString()); }
public string GetList(string userCode) { MyResponse response = new MyResponse(); try { List <Schedule> scList = db.Schedules.Where(X => X.UserCode == userCode).ToList(); string json = JsonConvert.SerializeObject(scList); response.Count = scList.Count.ToString(); response.Data = json; } catch (Exception ex) { if (ex.Message == StatusCode.ObjectNotFound.ToString()) { response.Code = StatusCode.ObjectNotFound; } else { response.Code = StatusCode.Error; } } return(response.ToString()); }
public async Task <HttpResponseMessage> UploadImage() { MyResponse response = new MyResponse(); string filePath = "~\\UploadFiles\\Photo"; string sort = "", imgInfo = "", userCode = ""; //获取传输过来的参数 try { sort = HttpContext.Current.Request.Form["sort"].ToString(); imgInfo = HttpContext.Current.Request.Form["info"].ToString(); userCode = HttpContext.Current.Request.Form["user"].ToString(); } catch { response.Code = StatusCode.ArgsNull; return(new HttpResponseMessage { Content = new StringContent(response.ToString(), System.Text.Encoding.UTF8, "application/json") }); } if (sort.Equals("head")) { filePath += "\\Head"; } else if (sort.Equals("live")) { filePath += "\\Live"; } else { response.Code = StatusCode.ArgsNull; return(new HttpResponseMessage { Content = new StringContent(response.ToString(), System.Text.Encoding.UTF8, "application/json") }); } // 取得文件夹 string dir = HttpContext.Current.Server.MapPath(filePath); //如果不存在文件夹,就创建文件夹 if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } if (!Request.Content.IsMimeMultipartContent("form-data")) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var provider = new CustomMultipartFormDataStreamProvider(dir); try { // Read the form data. await Request.Content.ReadAsMultipartAsync(provider); foreach (MultipartFileData file in provider.FileData) { //file.Headers.ContentDisposition.FileName;//上传文件前的文件名 //file.LocalFileName;//上传后的文件名 Photo p = new Photo(); p.ImgInfo = imgInfo; p.Sort = sort; p.AddUser = userCode; p.AddTime = DateTime.Now; p.Url = filePath + file.LocalFileName.Substring(file.LocalFileName.LastIndexOf("\\")); db.Photo.Add(p); db.SaveChanges(); //更新头像地址到用户表 if (sort.Equals("head") && !string.IsNullOrEmpty(userCode)) { User u = db.User.FirstOrDefault <User>(X => X.UserCode == userCode); if (u != null) { Photo newP = db.Photo.OrderByDescending(A => A.ID).FirstOrDefault(X => X.AddUser == u.UserCode); u.Photo = newP; db.Entry(u).State = EntityState.Modified; db.SaveChanges(); } } } //循环获取参数 //foreach (string key in provider.FormData.AllKeys) //{ // // dic.Add(key, provider.FormData[key]); //} response.Code = StatusCode.Success; } catch { response.Code = StatusCode.Error; } return(new HttpResponseMessage { Content = new StringContent(response.ToString(), System.Text.Encoding.UTF8, "application/json") }); }