Exemple #1
0
        public ActionResult Create(Request request, bool html = false)
        {
            request.CreatedOn = DateTime.UtcNow;
            var tags = TagsSplitter.Split(db, request.Tags);

            request.RequestTags = tags.Select(t => new RequestTag {
                Request = request, Tag = t
            }).ToList();
            if (ModelState.IsValid)
            {
                db.Requests.Add(request);
                db.SaveChanges();

                if (html)
                {
                    return(RedirectToAction("Details", new { id = request.RequestId }));
                }
                return(Json(new Response {
                    Data = new { request.RequestId }
                }));
            }

            if (html)
            {
                return(View(request));
            }
            return(Json(new Response {
                Data = "Failed"
            }));
        }
Exemple #2
0
        public ActionResult Create(User user, bool html = false)
        {
            user.CreatedOn = DateTime.UtcNow;
            var tags = TagsSplitter.Split(db, user.Tags);

            user.UserTags = tags.Select(t => new UserTag {
                User = user, Tag = t
            }).ToList();
            if (ModelState.IsValid)
            {
                db.Users.Add(user);
                db.SaveChanges();

                if (html)
                {
                    return(RedirectToAction("Details", new { id = user.UserId }));
                }
                return(Json(new Response {
                    Data = new { user.UserId }
                }));
            }

            if (html)
            {
                return(View(user));
            }
            return(Json(new Response {
                Data = "Failed"
            }));
        }
Exemple #3
0
        public ActionResult Edit(long id, Request request, bool html = false)
        {
            request.RequestId = id;
            var tags             = TagsSplitter.Split(db, request.Tags);
            var existingTags     = db.RequestTags.Where(rt => rt.RequestId == request.RequestId).Where(rt => rt.Tag != null).ToList();
            var existingTagNames = existingTags.Select(rt => rt.Tag.Name).ToList();

            foreach (var tag in tags.Where(t => !existingTagNames.Contains(t.Name)))
            {
                db.RequestTags.Add(new RequestTag()
                {
                    Request = request, Tag = tag
                });
            }
            foreach (var requestTag in existingTags.Where(rt => !tags.Select(t => t.Name).Contains(rt.Tag.Name)))
            {
                db.RequestTags.Remove(requestTag);
            }

            if (ModelState.IsValid)
            {
                db.Entry(request).State = EntityState.Modified;
                db.SaveChanges();

                if (html)
                {
                    return(RedirectToAction("Details", new { id = request.RequestId }));
                }
                return(Json(new Response {
                    Data = "Saved"
                }));
            }

            if (html)
            {
                return(View(request));
            }
            return(Json(new Response {
                Data = "Failed"
            }));
        }
Exemple #4
0
        public ActionResult Edit(long id, User user, bool html = false)
        {
            user.UserId = id;
            var tags             = TagsSplitter.Split(db, user.Tags);
            var existingTags     = db.UserTags.Where(ut => ut.UserId == user.UserId).Where(ut => ut.Tag != null).ToList();
            var existingTagNames = existingTags.Select(ut => ut.Tag.Name).ToList();

            foreach (var tag in tags.Where(t => !existingTagNames.Contains(t.Name)))
            {
                db.UserTags.Add(new UserTag {
                    User = user, Tag = tag
                });
            }
            foreach (var userTag in existingTags.Where(ut => !tags.Select(t => t.Name).Contains(ut.Tag.Name)))
            {
                db.UserTags.Remove(userTag);
            }

            if (ModelState.IsValid)
            {
                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();

                if (html)
                {
                    return(RedirectToAction("Details", new { id = user.UserId }));
                }
                return(Json(new Response {
                    Data = "Saved"
                }));
            }

            if (html)
            {
                return(View(user));
            }
            return(Json(new Response {
                Data = "Failed"
            }));
        }