コード例 #1
0
        public int Post(Models.UserFavorite value)
        {
            try
            {
                Data.TrnUserFavorite newUserFavorite = new Data.TrnUserFavorite();

                var  userId   = (from d in db.MstUsers where d.UserName.Equals(value.User) select d).FirstOrDefault().Id;
                bool isShared = false;

                DateTime    dt          = Convert.ToDateTime(value.EncodedDate);
                SqlDateTime EncodedDate = new SqlDateTime(new DateTime(dt.Year, dt.Month, dt.Day));

                newUserFavorite.UserId      = userId;
                newUserFavorite.Description = value.Description;
                newUserFavorite.IsShared    = isShared;
                newUserFavorite.EncodedDate = EncodedDate.Value;

                db.TrnUserFavorites.InsertOnSubmit(newUserFavorite);
                db.SubmitChanges();

                return(newUserFavorite.Id);
            }
            catch
            {
                return(0);
            }
        }
コード例 #2
0
        public HttpResponseMessage Delete(int Id)
        {
            Data.TrnUserFavorite deleteUserFavorite = db.TrnUserFavorites.Where(d => d.Id == Id).First();

            if (deleteUserFavorite != null)
            {
                var userFavoritesSymbols = from d in db.TrnUserFavoritesSymbols where d.UserFavoritesId == deleteUserFavorite.Id select d;
                if (userFavoritesSymbols.Any())
                {
                    foreach (Data.TrnUserFavoritesSymbol s in userFavoritesSymbols)
                    {
                        db.TrnUserFavoritesSymbols.DeleteOnSubmit(s);
                    }
                    try
                    {
                        db.SubmitChanges();
                    }
                    catch
                    {
                        // Do nothing.
                    }
                }

                db.TrnUserFavorites.DeleteOnSubmit(deleteUserFavorite);
                try
                {
                    db.SubmitChanges();
                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
                catch
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest));
                }
            }
            else
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }
        }