public override void HandleRequest()
        {
            CheckParameter("label_id");

            var label_id = new Guid(Parameters["label_id"]);

            if (label_id == Guid.Empty)
            {
                throw new Exception("Cannot delete reserved label: " + Parameters["label_id"]);
            }

            using (var db = new MyDbContext())
            {
                var label = (from lb in db.Object.Labels
                             where lb.label_id == label_id
                             select lb).FirstOrDefault();

                if (label == null)
                {
                    throw new Exception("label_id not exist:" + label_id);
                }

                label.deleted = true;
                label.seq     = SeqNum.GetNextSeq();

                db.Object.SaveChanges();
            }

            respondSuccess();
        }
Example #2
0
        public override void HandleRequest()
        {
            CheckParameter("label_id", "on_air");

            var label_id = new Guid(Parameters["label_id"]);
            var on_air   = (Parameters["on_air"] == "true");

            using (var db = new MyDbContext())
            {
                var q = (from f in db.Object.Labels
                         where f.label_id == label_id
                         select f).FirstOrDefault();

                if (q == null)
                {
                    throw new Exception("No such label: " + label_id.ToString());
                }

                q.on_air = on_air;
                q.seq    = SeqNum.GetNextSeq();

                db.Object.SaveChanges();
            }

            respondSuccess();
        }
        public override void HandleRequest()
        {
            CheckParameter("label_id", "name");

            var label_id = new Guid(Parameters["label_id"]);
            var name     = Parameters["name"];
            var file_ids = new List <Guid>();

            if (Parameters["file_id"] != null)
            {
                file_ids = Parameters["file_id"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => new Guid(x)).ToList();
            }


            using (var db = new MyDbContext())
            {
                var label = (from lb in db.Object.Labels
                             where lb.label_id == label_id
                             select lb).FirstOrDefault();

                if (label != null)
                {
                    throw new Exception("label_id already exist:" + label_id);
                }

                var seq = SeqNum.GetNextSeq();

                label = new Label
                {
                    label_id       = label_id,
                    name           = name,
                    seq            = seq,
                    share_proc_seq = seq,
                };

                db.Object.Labels.Add(label);


                if (file_ids.Any())
                {
                    foreach (var file_id in file_ids)
                    {
                        db.Object.LabelFiles.Add(new LabeledFile {
                            label_id = label_id, file_id = file_id
                        });
                    }
                }

                db.Object.SaveChanges();
            }

            respondSuccess();
        }
        public override void HandleRequest()
        {
            CheckParameter("label_id");

            var label_id = new Guid(Parameters["label_id"]);

            using (var conn = new SQLiteConnection(MyDbContext.ConnectionString))
            {
                conn.Open();


                using (var cmd = conn.CreateCommand())
                {
                    cmd.Connection  = conn;
                    cmd.CommandText = "delete from [LabelFiles] where label_id = @id";
                    cmd.CommandType = System.Data.CommandType.Text;

                    var par = cmd.CreateParameter();
                    par.ParameterName = "@id";
                    par.Value         = label_id;
                    cmd.Parameters.Add(par);

                    var nDeleted = cmd.ExecuteNonQuery();

                    if (nDeleted > 0)
                    {
                        using (var delCmd = conn.CreateCommand())
                        {
                            delCmd.CommandText = "update [Labels] set seq = @seq where label_id = @id";

                            var parSeq = delCmd.CreateParameter();
                            parSeq.Value         = SeqNum.GetNextSeq();
                            parSeq.ParameterName = "@seq";
                            delCmd.Parameters.Add(parSeq);
                            delCmd.Parameters.Add(new SQLiteParameter("@id", label_id));
                            delCmd.ExecuteNonQuery();
                        }
                    }
                }
            }

            respondSuccess();
        }
Example #5
0
        public override void HandleRequest()
        {
            CheckParameter("file_id", "label_id");

            var file_ids = Parameters["file_id"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => new Guid(x)).ToList();
            var label_id = new Guid(Parameters["label_id"]);

            using (var db = new MyDbContext())
            {
                var label = (from lb in db.Object.Labels
                             where lb.label_id == label_id
                             select lb).FirstOrDefault();

                if (label == null)
                {
                    throw new ArgumentException("no such label_id: " + label_id.ToString());
                }

                foreach (var file_id in file_ids)
                {
                    var labeledFile = (from lb in db.Object.LabelFiles
                                       where lb.label_id == label_id && lb.file_id == file_id
                                       select lb).FirstOrDefault();

                    if (labeledFile != null)
                    {
                        db.Object.LabelFiles.Remove(labeledFile);
                        label.seq = SeqNum.GetNextSeq();
                    }
                }

                db.Object.SaveChanges();
            }

            respondSuccess();
        }
Example #6
0
 public long GetNextSeq()
 {
     return(SeqNum.GetNextSeq());
 }
        public override void HandleRequest()
        {
            CheckParameter("file_id", "label_id");

            var file_ids = Parameters["file_id"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => new Guid(x)).ToList();
            var label_id = new Guid(Parameters["label_id"]);


            using (var conn = new SQLiteConnection(MyDbContext.ConnectionString))
            {
                conn.Open();

                using (var query = conn.CreateCommand())
                    using (var insert = conn.CreateCommand())
                    {
                        query.CommandText = "select 1 from [LabelFiles] where label_id = @label and file_id = @file";
                        query.Prepare();

                        insert.CommandText = "insert into [LabelFiles] (label_id, file_id) values (@label, @file)";
                        insert.Prepare();


                        using (var trans = conn.BeginTransaction())
                        {
                            bool inserted = false;

                            foreach (var file_id in file_ids)
                            {
                                var param_file  = new SQLiteParameter("@file", file_id);
                                var param_label = new SQLiteParameter("@label", label_id);
                                query.Parameters.Clear();
                                query.Parameters.Add(param_file);
                                query.Parameters.Add(param_label);

                                var alreadyTagged = (query.ExecuteScalar() != null);

                                if (alreadyTagged)
                                {
                                    continue;
                                }


                                insert.Parameters.Clear();
                                insert.Parameters.Add(param_file);
                                insert.Parameters.Add(param_label);
                                insert.ExecuteNonQuery();
                                inserted = true;
                            }

                            if (inserted)
                            {
                                using (var update = conn.CreateCommand())
                                {
                                    update.CommandText = "update [Labels] set seq = @seq where label_id = @label";
                                    update.Parameters.Add(new SQLiteParameter("@label", label_id));
                                    update.Parameters.Add(new SQLiteParameter("@seq", (object)SeqNum.GetNextSeq()));
                                    update.ExecuteNonQuery();
                                }
                            }

                            trans.Commit();
                        }
                    }
            }

            respondSuccess();
        }
        public override void HandleRequest()
        {
            CheckParameter("enabled", "label_id");

            var label_id = new Guid(Parameters["label_id"]);
            var enabled  = Convert.ToBoolean(Parameters["enabled"]);

            using (var db = new MyDbContext())
            {
                var q = from lb in db.Object.Labels
                        where lb.label_id == label_id
                        select lb;

                var label = q.FirstOrDefault();

                if (label == null)
                {
                    throw new Exception("no such label_id:" + label_id.ToString());
                }



                if (enabled)
                {
                    if (isShareEnabled(label))
                    {
                        respondSuccess(new { shared_code = label.share_code, status = 200, api_ret_code = 0, api_ret_message = "success" });
                        return;
                    }

                    if (string.IsNullOrEmpty(label.share_post_id))
                    {
                        var post_id     = Guid.NewGuid().ToString();
                        var api         = Cloud.CloudService.CreateCloudAPI();
                        var shared_code = api.NewPost(api.session_token, new List <string>(), new List <string>(), post_id, label.name);

                        label.share_post_id = post_id;
                        label.share_code    = shared_code;
                    }
                    else if (!string.IsNullOrEmpty(label.share_code) && (!label.share_enabled.HasValue || !label.share_enabled.Value))
                    {
                        var api = Cloud.CloudService.CreateCloudAPI();
                        api.tuneOnSharedcode(api.session_token, label.share_post_id);
                    }

                    label.share_enabled = enabled;
                }
                else
                {
                    if (!isShareEnabled(label))
                    {
                        respondSuccess();
                        return;
                    }

                    var api = Cloud.CloudService.CreateCloudAPI();
                    api.tuneOffSharedcode(api.session_token, label.share_post_id);
                    label.share_enabled = enabled;
                }

                label.seq = SeqNum.GetNextSeq();
                db.Object.SaveChanges();


                if (enabled)
                {
                    respondSuccess(new { shared_code = label.share_code, status = 200, api_ret_code = 0, api_ret_message = "success" });
                }
                else
                {
                    respondSuccess();
                }
            }
        }