NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

using Punjab_Shine.AdminManagerF;
using Punjab_Shine.DataAccess;
using Punjab_Shine.Models;
using Punjab_Shine.NewsManagerF;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace Punjab_Shine.Controllers
{
public class AdminController : Controller
{
AdminManager AdminManager;
NewsManager NewsManager;
public AdminController()
{
AdminManager = new AdminManager();
NewsManager = new NewsManager();
}

// GET: Admin
public ActionResult Index()
{
return View();
}
public ActionResult News()
{
if (Session["loggedinuser"] != null)
{
var logedInUser = (UserModel)Session["loggedinuser"];
return View();
}
return RedirectToAction("SignIn", "Login");
}

public ActionResult WebApi()
{
if (Session["loggedinuser"] != null)
{
var logedInUser = (UserModel)Session["loggedinuser"];
return View();
}
return RedirectToAction("SignIn", "Login");
}
[HttpGet]
public JsonResult GetNews()
{
List<GnewsModel> NewsList = new List<GnewsModel>();
try
{
using (EntityModel context = new EntityModel())
{
var newsList = context.News.ToList();
//var newsList = new List<News>();
HttpCookie cookie = HttpContext.Request.Cookies["Language"];

if (cookie != null && cookie.Value != "pa")
{

}

NewsList = newsList.Where(d => d.IsDeleted == false).OrderByDescending(d => d.CreatedDate).ThenByDescending(p => p.Priority).ToList().Select(x => new GnewsModel
{
NewsId = x.NewsId,
Desc = x.Desc,
shortDesc = x.shortDesc,
HasImage = x.HasImage,
ImageName = x.ImageName,
NewsPriority = (x.Priority == null ? 2 : Convert.ToInt32(x.Priority)),
author = (Convert.ToBoolean(x.IsApiNews) ? x.sourceName : "Admin"),
ImageUrl = x.ImageUrl,
publishedAt = (Convert.ToBoolean(x.IsApiNews) ? x.publishedAt : x.CreatedDate.ToString(@"yyyy/MM/dd hh:mm:ss tt", new System.Globalization.CultureInfo("en-US"))),
//PublishedTime = (Convert.ToBoolean(x.IsApiNews) ? Convert.ToDateTime(x.publishedAt) : x.CreatedDate),
sourceName = x.sourceName,
sourceUrl = x.sourceUrl,
sourceProviderUrl = x.sourceProviderUrl,
region = x.Region?.name,
RegionId = x.RegionId,
IsApiNews = (x.IsApiNews == null ? false : Convert.ToBoolean(x.IsApiNews))
}).ToList();
}
}
catch (Exception ex)
{
LogManagerF.LogManager.Log(ex, "Error in GetNews Function");
return Json("-1", JsonRequestBehavior.AllowGet);
}
return Json(NewsList, JsonRequestBehavior.AllowGet);
}

[HttpGet]
public JsonResult TryNewsApiFetch()
{
string region = "", url = "";
int RegionId = 0;
bool error = true, alreadyFetched = false, forceFetch = false;
Enums.RegionEnum Region = (Enums.RegionEnum)1;//default val
try
{
//HttpCookie cookie = HttpContext.Request.Cookies["Language"];

RegionId = Convert.ToInt32(Request.Params["RegionId"]);

forceFetch = Convert.ToBoolean(Request.Params["forceFetch"]);


//if (NewsType == "w")
//{
// region = "world";
// RegionId = (int)Enums.RegionEnum.india;
//}
//else if (NewsType == "p")
//{
// region = "punjab";
// RegionId = (int)Enums.RegionEnum.punjab;

//}
//else if (NewsType == "t")
//{
// region = "trending";
// RegionId = (int)Enums.RegionEnum.trending;

//}
//else if (NewsType == "punjab")
//{

if (RegionId > 0)
{

region = "punjab";

//RegionId = (int)Enums.RegionEnum.trending;

Region = (Enums.RegionEnum)RegionId;

Enums.RegionEnum RegionFromString = (Enums.RegionEnum)Enum.Parse(typeof(Enums.RegionEnum), "punjab");
//}
//else
//{
//}

using (EntityModel context = new EntityModel())
{
var newsList = context.News.Where(x => x.RegionId == RegionId && x.IsApiNews == true).ToList();

///if force fetch is not enabled we will check for date whether today current type of News are fetched already or not;
if (!forceFetch)
{
newsList = newsList.Where(x => x.CreatedDate.Date == DateTime.Now.Date).ToList();
}
/// count > 0 punjab news fetched already
if (newsList.Count > 0)
{
alreadyFetched = true;
}

var key = System.Configuration.ConfigurationManager.AppSettings["GnewsApiKey"];
if (RegionId == 7)
{
url = "https://gnews.io/api/v3/top-news" + "?&token=" + key;

}
else
{
url = "https://gnews.io/api/v3/search?q=" + Region.ToString() + "?&token=" + key;

}


error = false;

}
}
}
catch (Exception ex)
{
LogManagerF.LogManager.Log(ex, "Error in TryNewsApiFetch Function");
return Json("-1", JsonRequestBehavior.AllowGet);
}
return Json(new { regionId = RegionId, url = url, region = Region.ToString(), error = error, alreadyFetched = alreadyFetched }, JsonRequestBehavior.AllowGet);
}

[HttpGet]
public JsonResult GetHomeNews()
{

GnewsListingHolder gnewsListingHolder = new GnewsListingHolder();

try
{
var culture = Request.Params["culture"].ToString();
if (culture != "pa")
{
gnewsListingHolder = NewsManager.GetHomeNews(gnewsListingHolder, "en");
}
else
{
gnewsListingHolder = NewsManager.GetHomeNews(gnewsListingHolder, culture);
}
}
catch (Exception ex)
{
LogManagerF.LogManager.Log(ex, "Error in GetHomeNews Function");
return Json("-1", JsonRequestBehavior.AllowGet);
}
return Json(gnewsListingHolder, JsonRequestBehavior.AllowGet);
}

[HttpPost]
public async Task<JsonResult> SaveNewsUploadedFile()
{
//bool isSavedSuccessfully = true;
bool isFileExists = false;
string fName = "";
string ext = "";
var ImageName = Convert.ToString(Request.Params["ImageName"]);
var data = Request.Params["image"];
var randomName = Guid.NewGuid().ToString();
try
{
foreach (string fileName in Request.Files)
{
HttpPostedFileBase file = Request.Files[fileName];
//Save file content goes here
fName = ImageName == "" ? file.FileName : ImageName ?? file.FileName;
ext = Path.GetExtension(file.FileName);
//fName += ext;
fName = file.FileName;
fName = randomName.Substring(0, 7) + (ext == "" ? ".jpg" : ext);

if (file != null && file.ContentLength > 0)
{
//string pathString = System.IO.Path.Combine(originalDirectory.ToString(), ImageId + "\Document");
//SavedFileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);

var TemporaryPath = new DirectoryInfo(string.Format("{0}Images\Temp", Server.MapPath(@""))).ToString();
bool isTempDirExists = System.IO.Directory.Exists(TemporaryPath);
if (!isTempDirExists)
System.IO.Directory.CreateDirectory(TemporaryPath);

var TempSavingPath = string.Format("{0}\{1}", TemporaryPath, fName);

//isFileExists = System.IO.File.Exists(TempSavingPath);
/// SAving file in temp FOLDER
file.SaveAs(TempSavingPath);

var HeadPath = new DirectoryInfo(string.Format("{0}Images\Uploaded", Server.MapPath(@""))).ToString();
var HeadsavingPath = string.Format("{0}\{1}", HeadPath, fName);

bool isDirExists = System.IO.Directory.Exists(HeadPath);
if (!isDirExists)
System.IO.Directory.CreateDirectory(HeadPath);

isFileExists = System.IO.File.Exists(HeadsavingPath);
}
}
}
catch (Exception ex)
{
LogManagerF.LogManager.Log(ex, "Error in SaveNewsUploadedFile Function");
return Json(-1);
}

return Json(new { extension = ext, filename = fName, isfileExist = isFileExists });
}

[HttpPost]
public JsonResult SaveNews(NewsModel _NewsModel)
{
int _return = 0;
try
{
var logedInUser = (UserModel)Session["loggedinuser"];
if (logedInUser != null)
{

if (_NewsModel.NewsId == 0)
{
if (_NewsModel.HasImage == true)
{

var TemporaryPath = new DirectoryInfo(string.Format("{0}Images\Temp", Server.MapPath(@""))).ToString();
bool isTempDirExists = System.IO.Directory.Exists(TemporaryPath);
if (!isTempDirExists)
System.IO.Directory.CreateDirectory(TemporaryPath);

var TempSavingPath = string.Format("{0}\{1}", TemporaryPath, _NewsModel.ImageName);

//isFileExists = System.IO.File.Exists(TempSavingPath);
/// SAving file in temp FOLDER
var isFileExists = System.IO.File.Exists(TempSavingPath);

var HeadPath = new DirectoryInfo(string.Format("{0}Images\Uploaded", Server.MapPath(@""))).ToString();

var HeadsavingPath = string.Format("{0}\{1}", HeadPath, _NewsModel.ImageName);

bool isDirExists = System.IO.Directory.Exists(HeadPath);
if (!isDirExists)
System.IO.Directory.CreateDirectory(HeadPath);

System.IO.File.Copy(TempSavingPath, HeadsavingPath, true);
System.IO.File.Delete(TempSavingPath);

//if (SaveNewsDb(_NewsModel))
//{
// _return = 1;
//}
}
else
{

}

using (EntityModel context = new EntityModel())
{
DataAccess.News news = new DataAccess.News();
news.NewsId = _NewsModel.NewsId;
news.shortDesc = _NewsModel.shortDesc;
news.shortDescP = _NewsModel.shortDescP;
news.Desc = _NewsModel.Desc;
news.DescP = _NewsModel.DescP;
news.CreatedBy = logedInUser.UserId;
news.CreatedDate = DateTime.Now;
news.HasImage = _NewsModel.HasImage;
news.ImageName = _NewsModel.ImageName;
news.IsApiNews = false;
//news.region = "punjab";
news.RegionId = _NewsModel.RegionId;
//news.NewsPriority = newsModel.NewsPriority;
context.News.Add(news);
context.SaveChanges();
_return = 1;


}
}
else if (_NewsModel.NewsId != 0)
{
//var Template = TemplateManager.GetTemplateById(_NewsModel.NewsId);
var HeadPath = new DirectoryInfo(string.Format("{0}Images\Uploaded", Server.MapPath(@""))).ToString();

var TemporaryDirPath = new DirectoryInfo(string.Format("{0}Images\Temp", Server.MapPath(@""))).ToString();

var TempSavedFilePath = string.Format("{0}\{1}", TemporaryDirPath, _NewsModel.ImageName);
bool isTempFileExists = System.IO.File.Exists(TempSavedFilePath);

using (EntityModel context = new EntityModel())
{
Punjab_Shine.DataAccess.News news = context.News.Where(n => n.NewsId == _NewsModel.NewsId).FirstOrDefault();

var ImageNameEarlier = news.ImageName;


if (news != null)
{
news.shortDesc = _NewsModel.shortDesc;
news.shortDescP = _NewsModel.shortDescP;
news.Desc = _NewsModel.Desc;
news.DescP = _NewsModel.DescP;
news.HasImage = _NewsModel.HasImage;
news.ModifiedBy = logedInUser.UserId;
news.ModifiedDate = DateTime.Now;
news.Priority = _NewsModel.NewsPriority;
news.ImageName = _NewsModel.ImageName;
news.RegionId = _NewsModel.RegionId;

context.SaveChanges();


if (_NewsModel.ImageName != ImageNameEarlier)
{
/// name of the image changed & file exists
if (_NewsModel.HasImage || _NewsModel.ImageName != null)
{
if (isTempFileExists)
{
var NewSavingFilePath = string.Format("{0}\{1}", HeadPath, news.ImageName);

System.IO.File.Copy(TempSavedFilePath, NewSavingFilePath, true);

///Deleting temp file after changes
if (System.IO.File.Exists(NewSavingFilePath))
{
System.IO.File.Delete(TempSavedFilePath);
}
}
}
/// name of the image changed & file deleted
else if (_NewsModel.HasImage == false)
{
var HeadSavedFilePath = string.Format("{0}\{1}", HeadPath, ImageNameEarlier);

///Deleting temp file after changes
if (System.IO.File.Exists(HeadSavedFilePath))
{
System.IO.File.Delete(HeadSavedFilePath);
}
}
}
}
else
{
_return = -1;
LogManagerF.LogManager.Log(new Exception("news == null, no news found"), "Error in SaveNews Function");
}
}

}
}
else
{
_return = -5;
}
}
catch (Exception ex)
{
LogManagerF.LogManager.Log(ex, "Error in SaveNews Function");
_return = -1;
}
return Json(_return, JsonRequestBehavior.AllowGet);
}

[HttpGet]
public JsonResult GetNewsById()
{
NewsModel newsModel = null;
try
{
System.Threading.Thread.Sleep(1000);

//var path = HttpContext.Request.ApplicationPath;
var pathContext = HttpContext.Request?.Url?.AbsoluteUri;
Int32 NewsId = Convert.ToInt32(Request.Params["NewsId"]);
if (NewsId != 0)
{
var news = NewsManager.GetNewsFromDb(NewsId);

if (news != null)
{
newsModel = new NewsModel();
newsModel.NewsId = news.NewsId;
newsModel.shortDesc = news.shortDesc;
newsModel.Desc = news.Desc;
newsModel.DescP = news.DescP;
newsModel.shortDescP = news.shortDescP;
newsModel.HasImage = news.HasImage;
newsModel.ImageName = news.ImageName;
newsModel.NewsPriority = (news.Priority == null ? 2 : Convert.ToInt32(news.Priority));
}
}
}
catch (Exception ex)
{
LogManagerF.LogManager.Log(ex, "Error in GetNewsById Function");
return Json("-1", JsonRequestBehavior.AllowGet);
}
return Json(newsModel, JsonRequestBehavior.AllowGet);
}

[HttpGet]
public JsonResult DeleteNewsById()
{
var _return = -1;
try
{
Int32 NewsId = Convert.ToInt32(Request.Params["NewsId"]);
using (EntityModel context = new EntityModel())
{
var news = context.News.Where(x => x.NewsId == NewsId).FirstOrDefault();
if (news != null)
{
news.IsDeleted = true;
news.DeletedBy = 1;
news.DeletedDate = DateTime.Now;
context.SaveChanges();
_return = 1;
}
}
}
catch (Exception ex)
{
LogManagerF.LogManager.Log(ex, "Error in DeleteNewsById Function");
return Json("-1", JsonRequestBehavior.AllowGet);
}
return Json(_return, JsonRequestBehavior.AllowGet);
}

[HttpGet]
public ActionResult DownloadFile(string FilePath, int ImageId, int Option)
{
try
{
FilePath = FilePath.Replace("/", "\");
FilePath = System.IO.Path.Combine(Server.MapPath(@""), FilePath);
if (Option == 2 || Option == 1)
{
try
{
string PdfPath = System.IO.Path.Combine(Server.MapPath(@""), "Images\Temp\");
bool isExists = System.IO.Directory.Exists(PdfPath);
if (!isExists)
System.IO.Directory.CreateDirectory(PdfPath);
PdfPath = PdfPath + System.IO.Path.GetFileNameWithoutExtension(FilePath) + ".pdf";
if (!System.IO.File.Exists(PdfPath))
{
string CopyPath = System.IO.Path.Combine(Server.MapPath(@""), "Images\Temp\") + Guid.NewGuid().ToString().Substring(0, 12) + System.IO.Path.GetExtension(FilePath);
if (System.IO.File.Exists(FilePath))
{
System.IO.File.Copy(FilePath, CopyPath);
//if (WordToPDFConverter.GetWordToPDFConverter.CovertDocToPDF(CopyPath, PdfPath))
//{
// FilePath = PdfPath;
//}
}
}
}
catch (Exception ex)
{

LogManagerF.LogManager.Log(ex, "Error in DownloadFile Function");
}
}
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
Response.AddHeader("Content-Disposition", string.Format("attachment; filename = "{0}"", System.IO.Path.GetFileName(FilePath)));
response.TransmitFile(FilePath);
response.Flush();
response.End();
}
catch (Exception ex)
{
//LogManagerF.LogManager.Log(ex, "Error in DownloadFile Function");
}
return View();
}

[HttpPost]
public JsonResult SaveGNews(GnewsListingHolder _GnewsListingHolder)
{
int _return = 0;
try
{
if (Session["loggedinuser"] != null)
{
if (_GnewsListingHolder.LstGNews.Count > 0)
{
using (EntityModel context = new EntityModel())
{
foreach (var Gnews in _GnewsListingHolder.LstGNews)
{
DataAccess.News news = new DataAccess.News();
news.NewsId = Gnews.NewsId;
news.shortDesc = Gnews.shortDesc;
news.Desc = Gnews.Desc;
news.CreatedBy = 2;
news.CreatedDate = DateTime.Now;
news.HasImage = (Gnews.ImageUrl == null ? false : true);
news.ImageName = Gnews.ImageName;
news.sourceName = Gnews.sourceName;
news.sourceProviderUrl = Gnews.sourceProviderUrl;
news.sourceUrl = Gnews.sourceUrl;
news.publishedAt = Gnews.publishedAt.Substring(0, 19);
news.ImageUrl = Gnews.ImageUrl;
news.IsApiNews = true;
news.Priority = 4;
//news.region = _GnewsListingHolder.region;
news.RegionId = Gnews.RegionId;
context.News.Add(news);
}
context.SaveChanges();
}
}
//if (SaveGNews(_GnewsListingHolder))
//{
_return = 1;
//}
}
else
{
return Json("-5", JsonRequestBehavior.AllowGet);
}
}
catch (Exception ex)
{

LogManagerF.LogManager.Log(ex, "Error in SaveGnews Function");
_return = -1;
}
return Json(_return, JsonRequestBehavior.AllowGet);
}
}
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.