1
\$\begingroup\$

I read on SO and other pages that I should avoid JavascriptResult, I do not realy know why, but now I use JsonResult, because it is more common.

Here is my Ajax in the cshtml:

                        $.ajax({
                            type: "POST",
                            url: "@Url.Action("AddFolder")",
                            data: {
                                id: currentFolderObject.id,
                                folderId: _childFolderId,
                                newFolderName: data.text,
                                parent: _parentsChildId
                            },
                            success: function (serverData) {
                                if (!serverData.message === undefined) {
                                    if (!serverData.success) {
                                        alert(serverData.message);
                                    }
                                }
                                else {
                                    SetFolderModel(serverData.id, serverData.folderId, serverData.newFolderName, false, serverData.parent, "");

                                    if (!serverData.parent.match("_")) {
                                        FolderProperties(node, serverData.node);
                                    }
                                    else {
                                        $('#foldertree').jstree(true).refresh();
                                    }
                                }
                            },
                        })

So with that Ajax I am calling my AddFolder in my Controller. I think I am doing it rly complicated. On client side I have to use jstree which has a specific structure ( https://www.jstree.com/docs/json/ ). And that is why I am posting here.

    [HttpPost]
    public JsonResult AddFolder(int id, string folderId, string newFolderName, string parent)
    {
        bool publish;
        int mrNumber = 0;
        int parentFolder = Convert.ToInt32(GetFolderId(id).Split('_')[0]);
        int lastPeriod = GetLastPeriod();
        string mrDate = Helper.ConvertToIsoDate(DateTime.Now.ToString().Split(' ')[0]);

        if (lastPeriod == -1)
            throw new Exception(ErrorMessages.PeriodNotFound);

        if (parent == "#")
        {
            publish = true;
            try
            {
                folderId = (_ministerratRepository.GetLastMainFolderId() + 1).ToString();
            }
            catch (Exception ex)
            {
                Logging.LogToFile(ex, (int?)Helper.LogLevel.Error);
                return Json(new { success = false, message = ErrorMessages.CountFolderNotPossible }, JsonRequestBehavior.AllowGet);
            }
        }
        else
        {
            publish = false;
            if (parent.Contains("_"))
                mrNumber = GetLastMrNumber(lastPeriod, folderId);
            else
                mrNumber = GetLastMrNumber(lastPeriod, folderId) + 1;

            int folderIdIndex = 0;
            if (folderId.Count(x => x == '_') == 1)
            {
                folderIdIndex = GetLastFolderIdOfMinisterrat(folderId, parentFolder);

                if (Convert.ToInt32(folderId.Split('_')[1]) <= folderIdIndex)
                    folderId = parentFolder + "_" + Convert.ToInt32(folderIdIndex + 1);
            }
            else
            {
                folderIdIndex = GetLastFolderIdOfMinisterrat(folderId, parentFolder);
                if (Convert.ToInt32(folderId.Split('_')[2]) <= folderIdIndex)
                    folderId = parent + "_" + Convert.ToInt32(folderIdIndex + 1);
            }
        }

        if (mrNumber < 0)
            throw new Exception(ErrorMessages.MrNumberNotFound);

        try
        {
            id = _ministerratRepository.AddFolder(folderId, parent, lastPeriod, mrNumber, newFolderName, publish, mrDate);

            if (id == -1)
                throw new Exception(ErrorMessages.NoIdFound);
        }
        catch (NpgsqlException ex)
        {
            NpgsqlError(ex);
        }
        catch (Exception ex)
        {
            Logging.LogToFile(ex, (int?)Helper.LogLevel.Error);
            return Json(new { success = false, message = ErrorMessages.AddFolderNotPossible }, JsonRequestBehavior.AllowGet);
        }

        return Json(new { id, folderId, newFolderName, parent });
    }

Inside this AddFolder there are some other methods, they are basically returning integers from a DB query. I need folderId and parent to add the folder to the right layer. root (parent) is for example 26, child is 26_1, subchild is 26_1_1. I check that with the underscore.

For me it is looking very long to achieve the main goal to add a folder and I am not sure if i can achiev this easier because in my opinion this method is realy long in comparison to just Add a folder.

Furthermore I would like to know if my naming is correct (parameters, classes, methods) I am using here.

\$\endgroup\$
0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.