Ejemplo n.º 1
0
        public Operation <VideoModel> AddVideoOrUpdate(VideoModel model)
        {
            return(Operation.Create(() =>
            {
                if (model == null)
                {
                    _logger.LogError("Video cannot be empty");
                    throw new Exception("Video cannot be empty");
                }

                bool validate = model.Validate();
                if (!validate)
                {
                    _logger.LogError("Video ref cannot be empty");
                    throw new Exception("Video ref cannot be empty");
                }

                var query = _repo.Video.FirstOrDefault(c => c.Id == model.Id);

                if (query == null)
                {
                    query = _mapper.Map <VideoModel, Video>(model);
                    query.Points = model.Points;

                    _repo.Add <Video>(query);
                    _repo.SaveChanges();
                }
                else
                {
                    //query = _mapper.Map<VideoModel, Video>(model);

                    query.ThumbNail = model.ThumbNail ?? query.ThumbNail;
                    query.Title = model.Title == null ? query.Title : model.Title;
                    query.Views = model.Views <= 0 ? query.Views : model.Views;
                    query.Points = model.Points <= 0 ? query.Points : model.Points;
                    query.Author = model.Author ?? query.Author;

                    query.ModifiedBy = model.ModifiedBy.HasValue ? model.ModifiedBy.Value : 0;
                    query.ModifiedDate = DateTime.Now;

                    _repo.Update <Video>(query);
                    _repo.SaveChanges();
                }

                return _mapper.Map <Video, VideoModel>(query);
            }));
        }