Ejemplo n.º 1
0
        public IHttpActionResult Post([FromBody] CreateKMCModel model)
        {
            if (model.IsPublic)
            {
                ModelState.Remove("model.RoleIds");
            }

            if (model.IsEditor)
            {
                ModelState.Remove("model.File");
                ModelState.Remove("model.Type");
            }
            else
            {
                ModelState.Remove("model.EditorCode");
            }

            if (ModelState.IsValid)
            {
                var kmc = new KMCs
                {
                    KMCCategoryId = model.KMCCategoryId,
                    Title         = model.Title,
                    Description   = model.Description,
                    Thumbnail     = model.ThumbnailUrl,
                    IsEditor      = model.IsEditor,
                    KMCType       = model.Type,
                    FileId        = model.FileId,
                    FileType      = model.FileType,
                    EditorCode    = model.EditorCode,
                    IsPublic      = model.IsPublic,
                    IsShow        = model.IsShow,
                    CreatedBy     = model.CreatedBy,
                    CreatedDate   = DateTime.Now
                };

                if (!model.IsPublic && model.RoleIds != null)
                {
                    foreach (var roleid in model.RoleIds)
                    {
                        var kmcrole = new KMCRole
                        {
                            RoleId = roleid,
                            KMC    = kmc,
                        };

                        db.KMCRole.Add(kmcrole);
                    }
                }

                db.KMCs.Add(kmc);

                db.SaveChanges();

                return(Ok(kmc.Id));
            }

            return(BadRequest(ModelState));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Create(Models.CreateKMCModel model)
        {
            if (model.IsPublic)
            {
                ModelState.Remove("RoleIds");
            }

            if (model.IsEditor)
            {
                ModelState.Remove("File");
                ModelState.Remove("Type");
            }
            else
            {
                ModelState.Remove("EditorCode");

                //validation of file type
                if (model.File != null)
                {
                    var isValid = true;

                    switch (model.Type)
                    {
                    case KMCType.Image:
                        isValid = FileMethod.IsValidType(model.File, filter_imgs);
                        break;

                    case KMCType.Video:
                        isValid = FileMethod.IsValidType(model.File, filter_videos);
                        break;

                    case KMCType.Audio:
                        isValid = FileMethod.IsValidType(model.File, filter_audios);
                        break;

                    case KMCType.Document:
                        isValid = FileMethod.IsValidType(model.File, filter_docs);
                        break;

                    case KMCType.Others:

                        break;

                    default:
                        break;
                    }

                    if (!isValid)
                    {
                        ModelState.AddModelError("File", Language.KMC.ValidIsValidTypeFile);
                    }
                }
                else
                {
                    ModelState.AddModelError("FileName", Language.KMC.ValidRequiredFile);
                }
            }

            if (ModelState.IsValid)
            {
                var modelapi = new CreateKMCModel
                {
                    KMCCategoryId = model.CategoryId,
                    Title         = model.Title,
                    Description   = model.Description,
                    Type          = model.Type,
                    IsPublic      = model.IsPublic,
                    IsShow        = model.IsShow,
                    IsEditor      = model.IsEditor,
                    EditorCode    = model.EditorCode,
                    RoleIds       = model.RoleIds,
                    CreatedBy     = CurrentUser.UserId.Value
                };

                if (model.ThumbnailFile != null)
                {
                    var filename = FileMethod.SaveFile(model.ThumbnailFile, Server.MapPath("~/img/kmc-thumbnail"));
                    modelapi.ThumbnailUrl = filename;
                }

                if (model.File != null)
                {
                    var responseFile = await FileMethod.UploadFile(new List <HttpPostedFileBase> {
                        model.File
                    }, CurrentUser.UserId, "KMC/", model.File.ContentType);

                    if (responseFile != null)
                    {
                        modelapi.FileId   = responseFile.Select(f => f.Id).FirstOrDefault();
                        modelapi.FileType = model.File.ContentType;
                    }
                }

                var response = await WepApiMethod.SendApiAsync <int>(HttpVerbs.Post, $"KMC/Manage", modelapi);

                if (response.isSuccess)
                {
                    if (!model.IsPublic && model.IsShow)//send notification
                    {
                        var userIds = new List <int>();

                        foreach (var roleId in model.RoleIds)
                        {
                            var responseUsers = await WepApiMethod.SendApiAsync <List <UserModel> >(HttpVerbs.Get, $"Administration/Role/GetAllUser?roleId={roleId}");

                            if (responseUsers.isSuccess)
                            {
                                userIds = userIds.Union(responseUsers.Data.Select(r => r.Id).ToList()).ToList();
                            }
                        }

                        if (userIds.Count > 0)
                        {
                            ParameterListToSend notificationParameter = new ParameterListToSend();
                            notificationParameter.Link = $"<a href = '" + BaseURL + "/KMC/Home/Browse/" + response.Data.ToString() + "' > here </a>";

                            CreateAutoReminder notification = new CreateAutoReminder
                            {
                                NotificationType      = NotificationType.KMCCreated,
                                NotificationCategory  = NotificationCategory.Learning,
                                ParameterListToSend   = notificationParameter,
                                StartNotificationDate = DateTime.Now,
                                ReceiverId            = userIds
                            };

                            var responseNotification = await WepApiMethod.SendApiAsync <ReminderResponse>(HttpVerbs.Post, $"Reminder/SLA/GenerateAutoNotificationReminder/", notification);
                        }
                    }

                    await LogActivity(Modules.KMC, "Create KMC", model);

                    TempData["SuccessMessage"] = Language.KMC.AlertSuccessCreate;

                    return(RedirectToAction("List", "Manage", new { area = "KMC", @id = model.CategoryId }));
                }
                else
                {
                    TempData["ErrorMessage"] = Language.KMC.AlertFailCreate;
                }
            }

            model.filter_imgs   = filter_imgs;
            model.filter_videos = filter_videos;
            model.filter_audios = filter_audios;
            model.filter_docs   = filter_docs;

            return(View(model));
        }