Exemplo n.º 1
0
        public IActionResult SaveEntryTrg([FromForm] string entryId, [FromForm] string trg, [FromForm] string note)
        {
            if (entryId == null || trg == null || note == null)
            {
                return(StatusCode(400, "Missing parameter(s)."));
            }
            // Must be authenticated user
            int    userId;
            string userName;

            auth.CheckSession(HttpContext.Request.Headers, out userId, out userName);
            if (userId < 0)
            {
                return(StatusCode(401, "Request must contain authentication token."));
            }

            EditEntryResult res = new EditEntryResult();

            int idVal = EntryId.StringToId(entryId);

            trg = trg.Replace("\r\n", "\n");
            trg = trg.Replace('/', '\\');
            trg = trg.Replace('\n', '/');
            trg = "/" + trg + "/";
            using (SqlDict.SimpleBuilder builder = dict.GetSimpleBuilder(userId))
            {
                builder.ChangeTarget(userId, idVal, trg, note);
            }
            // Refresh cached contrib score
            auth.RefreshUserInfo(userId);
            // Tell our caller we dun it
            res.Success = true;
            return(new ObjectResult(res));
        }
Exemplo n.º 2
0
        public IActionResult SaveFullEntry([FromForm] string entryId, [FromForm] string hw, [FromForm] string trg,
                                           [FromForm] string note, [FromForm] string lang)
        {
            if (entryId == null || hw == null || trg == null || note == null || lang == null)
            {
                return(StatusCode(400, "Missing parameter(s)."));
            }

            // Must be authenticated user
            int    userId;
            string userName;

            auth.CheckSession(HttpContext.Request.Headers, out userId, out userName);
            if (userId < 0)
            {
                return(StatusCode(401, "Request must contain authentication token."));
            }

            EditEntryResult res = new EditEntryResult();

            int idVal = EntryId.StringToId(entryId);

            trg = trg.Replace("\r\n", "\n");
            trg = trg.Replace('/', '\\');
            trg = trg.Replace('\n', '/');
            trg = "/" + trg + "/";
            CedictParser parser = new CedictParser();
            CedictEntry  entry  = null;

            try { entry = parser.ParseEntry(hw + " " + trg, 0, null); }
            catch { }
            if (entry == null)
            {
                res.Error = TextProvider.Instance.GetString(lang, "editEntry.badDataOnSave");
                return(new ObjectResult(res));
            }

            bool persisted;

            using (SqlDict.SimpleBuilder builder = dict.GetSimpleBuilder(userId))
            {
                persisted = builder.ChangeHeadAndTarget(userId, idVal, hw, trg, note);
            }
            // Not persisted: violates uniqueness constraint
            if (!persisted)
            {
                res.Error = TextProvider.Instance.GetString(lang, "editEntry.duplicateOnSave");
                return(new ObjectResult(res));
            }

            // Refresh cached contrib score
            auth.RefreshUserInfo(userId);
            // Tell our caller we dun it
            res.Success = true;
            return(new ObjectResult(res));
        }
Exemplo n.º 3
0
        public IActionResult CreateEntry([FromForm] string simp, [FromForm] string trad,
                                         [FromForm] string pinyin, [FromForm] string trg, [FromForm] string note)
        {
            if (simp == null)
            {
                return(StatusCode(400, "Missing 'simp' parameter."));
            }
            if (trad == null)
            {
                return(StatusCode(400, "Missing 'trad' parameter."));
            }
            if (pinyin == null)
            {
                return(StatusCode(400, "Missing 'pinyin' parameter."));
            }
            if (trg == null)
            {
                return(StatusCode(400, "Missing 'trg' parameter."));
            }
            if (note == null)
            {
                return(StatusCode(400, "Missing 'note' parameter."));
            }

            // Must be authenticated user
            int    userId;
            string userName;

            auth.CheckSession(HttpContext.Request.Headers, out userId, out userName);
            if (userId < 0)
            {
                return(StatusCode(401, "Request must not contain authentication token."));
            }

            NewEntrySubmitResult res = new NewEntrySubmitResult {
                Success = true
            };

            SqlDict.SimpleBuilder builder = null;
            try
            {
                builder = dict.GetSimpleBuilder(userId);
                CedictEntry entry = Utils.BuildEntry(simp, trad, pinyin, trg);
                builder.NewEntry(entry, note);
                // Refresh cached contrib score
                auth.RefreshUserInfo(userId);
            }
            catch (Exception ex)
            {
                // TO-DO: Log
                //DiagLogger.LogError(ex);
                res.Success = false;
            }
            finally { if (builder != null)
                      {
                          builder.Dispose();
                      }
            }

            // Tell our caller
            return(new ObjectResult(res));
        }
Exemplo n.º 4
0
        public IActionResult CommentEntry([FromForm] string entryId, [FromForm] string note, [FromForm] string statusChange)
        {
            if (entryId == null || note == null || statusChange == null)
            {
                return(StatusCode(400, "Missing parameter(s)."));
            }
            // Supported/expected status changes
            SqlDict.Builder.StatusChange change;
            if (statusChange == "none")
            {
                change = SqlDict.Builder.StatusChange.None;
            }
            else if (statusChange == "approve")
            {
                change = SqlDict.Builder.StatusChange.Approve;
            }
            else if (statusChange == "flag")
            {
                change = SqlDict.Builder.StatusChange.Flag;
            }
            else if (statusChange == "unflag")
            {
                change = SqlDict.Builder.StatusChange.Unflag;
            }
            else
            {
                return(StatusCode(400, "Invalid statusChange parameter."));
            }

            // Must be authenticated user
            int    userId;
            string userName;

            auth.CheckSession(HttpContext.Request.Headers, out userId, out userName);
            if (userId < 0)
            {
                return(StatusCode(401, "Request must contain authentication token."));
            }

            // If status change is approve: is user entitled to do it?
            bool canApprove = false;

            if (change == SqlDict.Builder.StatusChange.Approve)
            {
                canApprove = auth.CanApprove(userId);
                if (!canApprove)
                {
                    return(StatusCode(401, "User is not authorized to approve entries."));
                }
            }

            bool success = false;

            SqlDict.SimpleBuilder builder = null;
            try
            {
                int idVal = EntryId.StringToId(entryId);
                builder = dict.GetSimpleBuilder(userId);
                builder.CommentEntry(idVal, note, change);
                // Refresh cached contrib score
                auth.RefreshUserInfo(userId);
                success = true;
            }
            catch (Exception ex)
            {
                // TO-DO: Log
                //DiagLogger.LogError(ex);
            }
            finally { if (builder != null)
                      {
                          builder.Dispose();
                      }
            }

            // Tell our caller
            return(new ObjectResult(success));
        }