public ActionResult ViewProfile(string userName) { if(string.IsNullOrEmpty(userName)) userName = HttpContext.User.Identity.Name; if(string.IsNullOrEmpty(userName)) return View(); UserRepository repo = new UserRepository(); UserModel user = repo.GetByUsernameWithPhotos(userName); UserProfileModel profile = null; if (user == null) { ViewBag.ErrorMessage = string.Format("Can't find user {0}", userName); return View(profile); } DateTime? startTime = user.DateOfBirth; string age = ""; if (startTime == null) age = "Unavailable"; else age = "" + Helpers.GetAge(startTime ?? DateTime.Today); profile = new UserProfileModel() { Name = user.Login, About = user.About, Age = age, Albums = new List<AlbumProfileModel>() }; string start,end; foreach (AlbumModel album in user.Albums) { Helpers.AlbumDateRange(album, out start , out end); AlbumProfileModel profileAlbum = new AlbumProfileModel() { Id = album.Id, Name = album.Name, Thumbnails = Helpers.AlbumThumbnails(album), StartDate = start, EndDate = end, Views = album.Views }; profile.Albums.Add(profileAlbum); } profile.Albums.Sort(delegate(AlbumProfileModel a, AlbumProfileModel b) { int aa=a.Id??0; int bb=b.Id??0; return aa.CompareTo(bb); }); return View(profile); }
public static List<AlbumProfileModel> Convert(List<AlbumModel> albums) { List<AlbumProfileModel> list = new List<AlbumProfileModel>(); string start, end; foreach (AlbumModel album in albums) { Helpers.AlbumDateRange(album, out start, out end); AlbumProfileModel profileAlbum = new AlbumProfileModel() { Id = album.Id, Name = album.Name, Thumbnails = Helpers.AlbumThumbnails(album), StartDate = start, EndDate = end, Views = album.Views, Comments = album.Comments.Count(delegate (CommentModel comment){ return comment.Accepted??false; }), Rating = album.Rating }; list.Add(profileAlbum); } return list; }
public ActionResult Browse(int? catId, int? pageNbr) { int maxItemsPerPage = 6; int nbr = pageNbr ?? 1; if (nbr < 1) nbr = 1; @ViewBag.Category = MainCategory.Browse; BrowseAlbumModel model = new BrowseAlbumModel(); CategoryRepository catRepo = new CategoryRepository(); model.Categories = catRepo.getCategories(); model.SelectedCategory = model.Categories.First().Id ?? 0; foreach (CategoryModel category in model.Categories) { if (category.Id == catId) { model.SelectedCategory = category.Id ?? 0; break; } } AlbumRepository albumRepo = new AlbumRepository(); AlbumModel[] albums = albumRepo.GetByCategory(catRepo.GetById(catId ?? model.Categories.First().Id), true, true, true, true, false).ToArray(); model.PageCount = (int)Math.Ceiling(albums.Length / (double)maxItemsPerPage); if (model.PageCount < 1) model.PageCount = 1; if (nbr > model.PageCount) nbr = model.PageCount; int start = (nbr - 1) * maxItemsPerPage; int end = nbr * maxItemsPerPage; model.CurrentPage = nbr; end = (end >= albums.Length) ? albums.Length : end; model.Albums = new List<AlbumProfileModel>(); string startS, endS; for (int i = start; i < end; ++i) { AlbumModel al = albums[i]; Helpers.AlbumDateRange(al, out startS, out endS); AlbumProfileModel album = new AlbumProfileModel { Comments = al.Comments.Count, Id = al.Id, Name = al.Name, Rating = al.Rating, Views = al.Views, StartDate = startS, EndDate = endS, Thumbnails = Helpers.AlbumThumbnails(al) }; model.Albums.Add(album); } model.Albums.Reverse(); return View(model); }