public PlayDataDTO(PlayDataDTO play)
 {
     PlayId     = play.PlayId;
     Title      = play.Title;
     Author     = play.Author;
     FestivalId = play.FestivalId;
     Day        = play.Day;
     Order      = play.Order;
     PlayedBy   = play.PlayedBy;
     Motto      = play.Motto;
 }
 public PlayDataDTO(PlayDataDTO play)
 {
     PlayId = play.PlayId;
     Title = play.Title;
     Author = play.Author;
     FestivalId = play.FestivalId;
     Day = play.Day;
     Order = play.Order;
     PlayedBy = play.PlayedBy;
     Motto = play.Motto;
 }
 // GET: /Festival/Details/5
 public ActionResult Details(int? id)
 {
     using (var context = new AF_Context())
     {
         if (id == null)
         {
             return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
         }
         var skip = 0;//pageAmount * (pageNr - 1);
         int pageAmount = 100;
         var query = (from p in context.Plays select p).Where(p => p.FestivalId == id);
         if (query == null)
         {
             return HttpNotFound();
         }
         List<PlayDataDTO> tmp = new List<PlayDataDTO>();
         foreach (Play pla in (query.OrderBy(p => p.FestivalId)
             .ThenBy(p => p.Day)
             .ThenBy(p => p.Order)
             .Skip(skip)
             .Take(pageAmount)))
         {
             var newPlayDto = new PlayDataDTO()
             {
                 PlayId = pla.PlayId,
                 Title = pla.Title,
                 Author = pla.Author,
                 FestivalId = pla.FestivalId,
                 Day = pla.Day,
                 Order = pla.Order,
                 PlayedBy = pla.PlayedBy,
                 Motto = pla.Motto
             };
             tmp.Add(newPlayDto);
         }
         return View(tmp);
     }
 }
 // GET: /Play/
 public ActionResult Index()
 {
     using (var context = new AF_Context())
     {
         //var plays = context.Plays;//.Include(p => p.Editor).Include(p => p.Festival);
         //return View(plays.ToList());
         var skip = 0;//pageAmount * (pageNr - 1);
         int pageAmount = 100;
         var query = (from p in context.Plays select p);
         if (query == null)
         {
             return HttpNotFound();
         }
         List<PlayDataDTO> tmp = new List<PlayDataDTO>();
         foreach (Play pla in (query.OrderBy(p => p.FestivalId)
             .ThenBy(p => p.Day)
             .ThenBy(p => p.Order)
             .Skip(skip)
             .Take(pageAmount)))
         {
             var newPlayDto = new PlayDataDTO()
             {
                 PlayId = pla.PlayId,
                 Title = pla.Title,
                 Author = pla.Author,
                 FestivalId = pla.FestivalId,
                 Day = pla.Day,
                 Order = pla.Order,
                 PlayedBy = pla.PlayedBy,
                 Motto = pla.Motto
             };
             tmp.Add(newPlayDto);
         }
         return View(tmp);
     }
 } 
 public PlayEditViewModel(List<int> fesitvalsList)
 {
     FestivalsList = fesitvalsList;
     EditedPlay = new PlayDataDTO();
 }
 public PlayEditWindow(PlayDataDTO editedPlay, List<int> fesitvalsList)
 {
     PEViewModel = new PlayEditViewModel(fesitvalsList){OriginalPlay = editedPlay};
     InitializeComponent();
 }
        public ListResponse<PlayDataDTO> SearchPlays(PlaysSearchingCriteria criteria, int pageNr, int pageAmount)
        {
            using (var context = new AF_Context())
            {
                try
                {
                    var skip = pageAmount*(pageNr - 1);
                    var query = (from p in context.Plays select p);
                    if (criteria.FestivalIdFilter != null)
                        query = query.Where(p => p.FestivalId == criteria.FestivalIdFilter);
                    if (!String.IsNullOrEmpty(criteria.Author))
                        query = query.Where(p => p.Author.Contains(criteria.Author));
                    if (!String.IsNullOrEmpty(criteria.Title))
                        query = query.Where(p => p.Title.Contains(criteria.Title));
                    if (!String.IsNullOrEmpty(criteria.Motto))
                        query = query.Where(p => p.Motto.Contains(criteria.Motto));

                    List<PlayDataDTO> tmp = new List<PlayDataDTO>();
                    foreach (Play pla in (query.OrderBy(p => p.FestivalId)
                        .ThenBy(p => p.Day)
                        .ThenBy(p => p.Order)
                        .Skip(skip)
                        .Take(pageAmount)))
                    {
                        var newPlayDto = new PlayDataDTO()
                        {
                            PlayId = pla.PlayId,
                            Title = pla.Title,
                            Author = pla.Author,
                            FestivalId = pla.FestivalId,
                            Day = pla.Day,
                            Order = pla.Order,
                            PlayedBy = pla.PlayedBy,
                            Motto = pla.Motto
                        };
                        tmp.Add(newPlayDto);
                    }
                    return (new ListResponse<PlayDataDTO>(tmp));
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public SingleItemResponse<PlayDataDTO> GetPlay(int id)
        {
            using (var context = new AF_Context())
            {
                try
                {
                    Play pla = context.Plays.First(p => p.PlayId == id);

                    var newPlayDto = new PlayDataDTO()
                    {
                        PlayId = pla.PlayId,
                        Title = pla.Title,
                        Author = pla.Author,
                        FestivalId = pla.FestivalId,
                        Day = pla.Day,
                        Order = pla.Order,
                        PlayedBy = pla.PlayedBy,
                        Motto = pla.Motto
                    };
                    return (new SingleItemResponse<PlayDataDTO>(newPlayDto));
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public SingleItemResponse<PlayDataDTO> UpdatePlay(PlayDataDTO updateData)
        {
            var updateDataFull = new Play()
            {
                PlayId = updateData.PlayId,
                Title = updateData.Title,
                Author = updateData.Author,
                FestivalId = updateData.FestivalId,
                Day = updateData.Day,
                Order = updateData.Order,
                PlayedBy = updateData.PlayedBy,
                Motto = updateData.Motto,
                EditedBy = GetUserId(),
                EditDate = DateTime.Now
            };

            using (var context = new AF_Context())
            {
                try
                {
                    Play pla = context.Plays.First(p => p.PlayId == updateData.PlayId);
                    context.Entry(pla).CurrentValues.SetValues(updateDataFull);
                    context.SaveChanges();
                    int id = updateData.PlayId;
                    return GetPlay(id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
        public SingleItemResponse<PlayDataDTO> AddPlay(PlayDataDTO newPlay)
        {
            var newPlayFull = new Play()
            {
                Title = newPlay.Title,
                Author = newPlay.Author,
                FestivalId = newPlay.FestivalId,
                Day = newPlay.Day,
                Order = newPlay.Order,
                PlayedBy = newPlay.PlayedBy,
                Motto = newPlay.Motto,
                EditedBy = GetUserId(),
                EditDate = DateTime.Now
            };

            using (var context = new AF_Context())
            {
                try
                {
                    context.Plays.Add(newPlayFull);
                    context.SaveChanges();
                    int id = newPlayFull.PlayId;
                    return GetPlay(id);
                }
                catch (Exception ex)
                {
                    throw;
                }
            }
        }
 public ActionResult Create()
 {
     var tmp = new PlayDataDTO(){FestivalId=18}; //to change
     //ViewBag.FestivalId = 18;
     //ViewBag.EditedBy = new SelectList(db.Users, "UserId", "Login");
     return View(tmp);
 }
 public ActionResult Edit(int? id)
 {
     if (id == null)
     {
         return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
     }
     using (var context = new AF_Context())
     {
         Play pla = context.Plays.Find(id);// First(p => p.PlayId == id);
         if (pla == null)
         {
             return HttpNotFound();
         }
         var newPlayDto = new PlayDataDTO()
         {
             PlayId = pla.PlayId,
             Title = pla.Title,
             Author = pla.Author,
             FestivalId = pla.FestivalId,
             Day = pla.Day,
             Order = pla.Order,
             PlayedBy = pla.PlayedBy,
             Motto = pla.Motto
         };
         //ViewBag.EditedBy = new SelectList(db.Users, "UserId", "Login", festival.EditedBy);
         return View(newPlayDto);
     }   
 }