예제 #1
0
        public async Task <IActionResult> Login([FromBody] LoginInput reqData)
        {
            int resHttpStatusCode                    = 400;
            Dictionary <string, string> res          = new Dictionary <string, string>();
            Func <string, Task <bool> > authverifier = null;
            string authverifierToken                 = "";

            switch (reqData.provider)
            {
            case "GOOGLE":
                authverifier      = AuthToken.VerifyGoogleOauthV2Token;
                authverifierToken = reqData.idToken;
                break;

            case "FACEBOOK":
                authverifier      = AuthToken.VerifyFacebookOauthToken;
                authverifierToken = reqData.authToken;
                break;
            }
            if (authverifier != null && await authverifier(authverifierToken).ConfigureAwait(true))
            {
                int userSocialId = 0;
                using (var ctx = new ConclaveDbContext())
                {
                    var entity = from u in ctx.UserSocial where u.Email == reqData.email && u.Provider == reqData.provider select u;
                    if (entity.FirstOrDefault() != null)
                    {
                        userSocialId = entity.First().Id;
                    }
                    else
                    {
                        UserSocial user = new UserSocial()
                        {
                            Provider = reqData.provider,
                            Email    = reqData.email,
                            UserName = reqData.name
                        };
                        ctx.Add(user);
                        ctx.SaveChanges();
                        userSocialId = user.Id;
                    }
                }

                string accessToken  = AuthToken.GetNewAccessToken(userSocialId);
                string refreshToken = AuthToken.GetNewRefreshToken(userSocialId);
                res.Add("success", "true");
                res.Add("email", reqData.email);
                res.Add("accessToken", accessToken);
                res.Add("refreshToken", refreshToken);
                _cache.StringSet(accessToken, userSocialId.ToString());
                _cache.StringSet(refreshToken, userSocialId.ToString());
                resHttpStatusCode = 200;
            }
            else
            {
                res.Add("success", "false");
                res.Add("msg", "Invalid Token");
            }
            return(StatusCode(resHttpStatusCode, JsonConvert.SerializeObject(res)));
        }
예제 #2
0
        public IActionResult Delete(int id)
        {
            int    resHttpStatusCode = 400;
            string resMsg            = "Bad request";

            try
            {
                using (var ctx = new ConclaveDbContext())
                {
                    var itemToRemove = ctx.Post.SingleOrDefault(x => x.Id == id);
                    if (itemToRemove != null)
                    {
                        if (itemToRemove.Media == "Y")
                        {
                            var q = (from item in ctx.PostMedia where item.PostId == id select new { id = item.Id }).ToList();
                            foreach (var item in q)
                            {
                                PostMedia pm = new PostMedia()
                                {
                                    Id = item.id
                                };
                                ctx.PostMedia.Attach(pm);
                                ctx.PostMedia.Remove(pm);
                            }
                            ctx.SaveChanges();
                        }
                        ctx.Post.Attach(itemToRemove);
                        ctx.Post.Remove(itemToRemove);
                        ctx.SaveChanges();
                        _cache.StringSet("posts-get-all-isnew", "y");
                        resHttpStatusCode = 200;
                    }
                }
            }
            catch (Exception e)
            {
                CLogger.Log(e);
                resHttpStatusCode = 500;
            }

            HttpContext.Response.StatusCode = resHttpStatusCode;
            return(new JsonResult(new
            {
                success = (resHttpStatusCode == 200 ? "true" : "false"),
                msg = resMsg
            }));
        }
예제 #3
0
        public async Task <IActionResult> Put(int id)
        {
            int    resHttpStatusCode = 400;
            string resMsg            = "Bad request";

            try
            {
                using (var ctx = new ConclaveDbContext())
                {
                    Post post = (from p in ctx.Post where p.Id == id select p).FirstOrDefault();
                    if (post != null)
                    {
                        var uploadPath = _config["AppConfig:Storage:Uploads"];

                        if (Request.Form["text"].ToString() != "")
                        {
                            post.Text = Request.Form["text"].ToString();
                        }

                        if (Request.Form.Files.Count > 0)
                        {
                            List <Tuple <string, string> > attachmentList = new List <Tuple <string, string> >();
                            foreach (var formFile in Request.Form.Files)
                            {
                                if (formFile.Length > 0)
                                {
                                    var filetype = formFile.ContentType.Split("/")[0];
                                    var filename = Guid.NewGuid().ToString() + DateTime.UtcNow.ToString("yyyyMMddHHmmsss") + formFile.FileName.Substring(formFile.FileName.LastIndexOf(".", System.StringComparison.Ordinal));
                                    var filePath = Path.Combine(uploadPath, filename);
                                    var origin   = Request.Scheme + "://" + Request.Host;

                                    using (var stream = System.IO.File.Create(filePath))
                                    {
                                        await formFile.CopyToAsync(stream).ConfigureAwait(false);

                                        attachmentList.Add(Tuple.Create(filetype, origin + "/" + uploadPath + filename));
                                    }
                                }
                            }
                            post.Media = "Y";
                            ctx.PostMedia.RemoveRange(ctx.PostMedia.Where(x => x.PostId == id));
                        }
                        ctx.SaveChanges();
                        _cache.StringSet("posts-get-all-isnew", "y");
                        resHttpStatusCode = 200;
                        resMsg            = "Post updated successfully";
                    }
                }
            }
            catch (Exception e)
            {
                CLogger.Log(e);
                resHttpStatusCode = 500;
                resMsg            = "An internal error has occurred";
            }

            HttpContext.Response.StatusCode = resHttpStatusCode;
            return(new JsonResult(new
            {
                success = (resHttpStatusCode == 200 ? "true" : "false"),
                msg = resMsg
            }));
        }
예제 #4
0
        public async Task <IActionResult> Post()
        {
            int    resHttpStatusCode = 400;
            string resMsg            = "Bad request";

            try
            {
                var uploadPath = _config["AppConfig:Storage:Uploads"];
                if (!string.IsNullOrEmpty(Request.Form["text"].ToString()))
                {
                    Post post = new Post();
                    post.UserSocialId = AuthToken.GetUserSocialId(HttpContext.Request.Headers["Authorization"].ToString().Split(" ")[1]);
                    post.Text         = Request.Form["text"].ToString();

                    List <Tuple <string, string> > attachmentList = new List <Tuple <string, string> >();
                    foreach (var formFile in Request.Form.Files)
                    {
                        if (formFile.Length > 0)
                        {
                            var filetype = formFile.ContentType.Split("/")[0];
                            var filename = Guid.NewGuid().ToString() + DateTime.UtcNow.ToString("yyyyMMddHHmmsss") + formFile.FileName.Substring(formFile.FileName.LastIndexOf(".", System.StringComparison.Ordinal));
                            var filePath = Path.Combine(uploadPath, filename);

                            using (var stream = System.IO.File.Create(filePath))
                            {
                                await formFile.CopyToAsync(stream).ConfigureAwait(false);

                                attachmentList.Add(Tuple.Create(filetype, "/" + uploadPath + filename));
                            }
                        }
                    }
                    post.Media = attachmentList.Count() > 0 ? "Y" : "N";
                    using (var ctx = new ConclaveDbContext())
                    {
                        ctx.Post.Add(post);
                        ctx.SaveChanges();
                        var postid = post.Id;
                        foreach (var(filetype, filepath) in attachmentList)
                        {
                            PostMedia pm = new PostMedia();
                            pm.PostId   = postid;
                            pm.Path     = filepath;
                            pm.Filetype = filetype;
                            ctx.PostMedia.Add(pm);
                            ctx.SaveChanges();
                        }
                    }
                    _cache.StringSet("posts-get-all-isnew", "y");
                    resHttpStatusCode = 200;
                    resMsg            = "Post addedd successfully";
                }
            }
            catch (Exception e)
            {
                resHttpStatusCode = 500;
                resMsg            = "An internal error has occurred";
                CLogger.Log(e);
            }
            HttpContext.Response.StatusCode = resHttpStatusCode;
            return(new JsonResult(new
            {
                success = (resHttpStatusCode == 200 ? "true" : "false"),
                msg = resMsg
            }));;
        }