Skip to content

Commit 4f6c3c9

Browse files
author
Guus Beltman
committed
Added excel export + improved homepage
1 parent 15e14ba commit 4f6c3c9

File tree

13 files changed

+188
-39
lines changed

13 files changed

+188
-39
lines changed

BookCollection/App_Start/RouteConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static void RegisterRoutes(RouteCollection routes)
1616
routes.MapRoute(
1717
name: "Default",
1818
url: "{controller}/{action}/{id}",
19-
defaults: new { controller = "Books", action = "Index", id = UrlParameter.Optional }
19+
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
2020
);
2121

2222
}

BookCollection/BookCollection.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@
212212
</Compile>
213213
<Compile Include="Helpers\BC.cs" />
214214
<Compile Include="Helpers\Converters.cs" />
215+
<Compile Include="Helpers\DownloadFileActionResult.cs" />
215216
<Compile Include="Helpers\Validators.cs" />
216217
<Compile Include="Logging\ILogger.cs" />
217218
<Compile Include="Logging\TraceLogger.cs" />
@@ -231,7 +232,7 @@
231232
<Content Include="Content\bootstrap.css" />
232233
<Content Include="Content\bootstrap.min.css" />
233234
<Content Include="Content\PagedList.css" />
234-
<Content Include="Content\Tagcloud.css" />
235+
<Content Include="Content\HomeIndex.css" />
235236
<Content Include="favicon.ico" />
236237
<Content Include="fonts\glyphicons-halflings-regular.svg" />
237238
<Content Include="Global.asax" />
@@ -491,6 +492,7 @@
491492
<Content Include="Images\Flags\za.png" />
492493
<Content Include="Images\Flags\zm.png" />
493494
<Content Include="Images\Flags\zw.png" />
495+
<Content Include="Images\homepage.jpg" />
494496
<Content Include="Images\Logos\bookcase128.ico" />
495497
<Content Include="Images\Logos\bookcase128.png" />
496498
<Content Include="Images\Logos\bookcase16.ico" />
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
------------------------------
3+
Jumbotron background image
4+
------------------------------
5+
*/
6+
7+
.bg {
8+
background: url('/Images/homepage.jpg') no-repeat center center;
9+
position: fixed;
10+
width: 100%;
11+
height: 350px; /*same height as jumbotron */
12+
top:0;
13+
left:0;
14+
z-index: -1;
15+
}
16+
17+
.jumbotron {
18+
margin-bottom: 0px;
19+
height: 350px;
20+
color: white;
21+
text-shadow: black 0.3em 0.3em 0.3em;
22+
background:transparent;
23+
}
24+
25+
/*
26+
------------------------------
27+
Tag Cloud
28+
------------------------------
29+
*/
30+
31+
.tag1{font-size: 0.8 em}
32+
.tag2{font-size: 0.9em}
33+
.tag3{font-size: 1em}
34+
.tag4{font-size: 1.2em}
35+
.tag5{font-size: 1.4em}
36+
.tag6{font-size: 1.7em}
37+
.tag7{font-size: 2.0em}

BookCollection/Content/Tagcloud.css

Lines changed: 0 additions & 13 deletions
This file was deleted.

BookCollection/Controllers/HomeController.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using BookCollection.DAL;
2+
using BookCollection.Helpers;
23
using BookCollection.ViewModels;
34
using System;
45
using System.Collections.Generic;
56
using System.Linq;
67
using System.Web;
78
using System.Web.Mvc;
9+
using System.Web.UI.WebControls;
810

911
namespace BookCollection.Controllers
1012
{
@@ -13,11 +15,26 @@ public class HomeController : BaseController
1315
public ActionResult Index()
1416
{
1517
// http://www.mikesdotnetting.com/article/107/creating-a-tag-cloud-using-asp-net-mvc-and-the-entity-framework
16-
ViewData["TagCloud"] = repo.GetBookCategories();
18+
19+
ViewBag.CatTagCloud = repo.GetBookCategories();
20+
ViewBag.SerieTagCloud = repo.GetBookSeries();
21+
ViewBag.MostRecent = repo.GetMostRecentBooks(5);
22+
// ViewData = ViewData is a dictionary object that you put data into, which then becomes available to the view. ViewData is a derivative of the ViewDataDictionary class, so you can access by the familiar "key/value" syntax.
23+
// ViewBag = The ViewBag object is a wrapper around the ViewData object that allows you to create dynamic properties for the ViewBag.
1724

1825
return View();
1926
}
2027

28+
public ActionResult DownloadExcel()
29+
{
30+
var data = db.Books.ToList();
31+
GridView gv = new GridView();
32+
gv.DataSource = data;
33+
gv.DataBind();
34+
35+
return new DownloadFileActionResult(gv, "Books.xls");
36+
}
37+
2138
public ActionResult About()
2239
{
2340
Statistics stats = new Statistics();
@@ -58,6 +75,8 @@ group books by books.Language into catGroup
5875

5976
return View(stats);
6077
}
78+
79+
6180

6281
}
6382
}

BookCollection/DAL/BookRepository.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ namespace BookCollection.DAL
1111
public interface IBookRepository
1212
{
1313
void SetContext(BookContext bookContext);
14+
IEnumerable<Book> GetMostRecentBooks(int size);
1415
IEnumerable<CategoryGroup> GetBookCategories();
16+
IEnumerable<CategoryGroup> GetBookSeries();
1517
}
1618

1719
public class BookRepository : IBookRepository
@@ -22,6 +24,11 @@ public void SetContext(BookContext bookContext)
2224
db = bookContext;
2325
}
2426

27+
public IEnumerable<Book> GetMostRecentBooks(int size)
28+
{
29+
return db.Books.OrderByDescending(b => b.CreationDate).Take(size);
30+
}
31+
2532
public IEnumerable<CategoryGroup> GetBookCategories()
2633
{
2734
var totalBooks = db.Books.Count();
@@ -34,7 +41,24 @@ group books by books.Category.Title into catGroup
3441
TotalBookCount = totalBooks
3542
};
3643

37-
return taglist;
44+
return taglist.Where(c => c.CategoryName != "");
45+
}
46+
47+
public IEnumerable<CategoryGroup> GetBookSeries()
48+
{
49+
var totalBooks = db.Books.Count();
50+
var taglist = from books in db.Books
51+
group books by books.Serie into catGroup
52+
select new CategoryGroup()
53+
{
54+
CategoryName = catGroup.Key,
55+
BookCount = catGroup.Count(),
56+
TotalBookCount = totalBooks
57+
};
58+
59+
return taglist.Where(c => c.CategoryName != "");
3860
}
61+
62+
3963
}
4064
}

BookCollection/DAL/SeedData/CsvSeedDataProvider.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ namespace BookCollection.DAL
1111
{
1212
public class CsvSeedDataProvider : ISeedDataProvider
1313
{
14+
private const string resourceName = "BookCollection.DAL.SeedData.basicseeddata.csv";
15+
1416
public IEnumerable<seedDataModel> GetData()
1517
{
1618
var dataRows = new List<seedDataModel>();
1719
Assembly assembly = Assembly.GetExecutingAssembly();
18-
string resourceName = "BookCollection.DAL.SeedData.madbooks_seeddata.csv";
20+
1921
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
2022
{
2123
using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Web;
7+
using System.Web.Mvc;
8+
using System.Web.UI;
9+
using System.Web.UI.WebControls;
10+
11+
namespace BookCollection.Helpers
12+
{
13+
public class DownloadFileActionResult : ActionResult
14+
{
15+
16+
public GridView ExcelGridView { get; set; }
17+
public string fileName { get; set; }
18+
19+
20+
public DownloadFileActionResult(GridView gv, string pFileName)
21+
{
22+
ExcelGridView = gv;
23+
fileName = pFileName;
24+
}
25+
26+
27+
public override void ExecuteResult(ControllerContext context)
28+
{
29+
30+
//Create a response stream to create and write the Excel file
31+
HttpContext curContext = HttpContext.Current;
32+
curContext.Response.Clear();
33+
curContext.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
34+
curContext.Response.Charset = "";
35+
curContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
36+
curContext.Response.ContentType = "application/vnd.ms-excel";
37+
38+
//Convert the rendering of the gridview to a string representation
39+
StringWriter sw = new StringWriter();
40+
HtmlTextWriter htw = new HtmlTextWriter(sw);
41+
ExcelGridView.RenderControl(htw);
42+
43+
//Open a memory stream that you can use to write back to the response
44+
byte[] byteArray = Encoding.ASCII.GetBytes(sw.ToString());
45+
MemoryStream s = new MemoryStream(byteArray);
46+
StreamReader sr = new StreamReader(s, Encoding.ASCII);
47+
48+
//Write the stream back to the response
49+
curContext.Response.Write(sr.ReadToEnd());
50+
curContext.Response.End();
51+
52+
}
53+
54+
}
55+
}

BookCollection/Images/homepage.jpg

91.3 KB
Loading

BookCollection/Views/Books/Index.cshtml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<div class="col-md-6"><h2>@ViewBag.Title</h2></div>
1212
<div class="col-md-6">
1313
<div class="pull-right" style="margin:25px 5px 0 0">
14-
@BC.IconButton("Create", "plus", "Add new book")
14+
@BC.IconButton("Books", "Create", "plus", "Add new book")
1515
</div>
1616
</div>
1717
</div>
@@ -80,16 +80,17 @@
8080
@Html.DisplayFor(modelItem => item.Serie)
8181
</td>
8282
<td class="text-nowrap">
83-
@BC.IconButton("Edit", "pencil", "Edit", item.BookID)
84-
@BC.IconButton("Details", "eye-open", "Details", item.BookID)
85-
@BC.IconButton("Delete", "remove", "Delete", item.BookID)
83+
@BC.IconButton("Books", "Edit", "pencil", "Edit", item.BookID)
84+
@BC.IconButton("Books", "Details", "eye-open", "Details", item.BookID)
85+
@BC.IconButton("Books", "Delete", "remove", "Delete", item.BookID)
8686
</td>
8787
</tr>
8888
}
8989

9090
</table>
9191

92+
9293
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
9394

9495
@Html.PagedListPager(Model, page => Url.Action("Index",
95-
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
96+
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

0 commit comments

Comments
 (0)