Ejemplo n.º 1
0
 private Topic CreateModel(TopicBindingModel model, Topic topic)
 {
     topic.Name             = model.Name;
     topic.Numberofmessages = model.NumberOfMessages;
     topic.Numberofvisitors = model.NumberOfVisitors;
     return(topic);
 }
Ejemplo n.º 2
0
        public async Task <IHttpActionResult> UpdatePost([FromBody] TopicBindingModel model)
        {
            try
            {
                ApplicationUser user = this.userRepository.GetOneById(User.Identity.GetUserId());

                Topic t = this.repository.GetOneById(model.Id);

                if (t.creatorId == user.Id || User.IsInRole("Admin"))
                {
                    repository.UpdateOne(new Topic {
                        Id = model.Id, Title = model.Title, CreationDate = model.CreationDate
                    });
                    return(Ok("Topic Updated"));
                }
                else
                {
                    return(BadRequest("Cannot update a Topic which you are not an admin of!"));
                }
            }
            catch
            {
                return(BadRequest("Bad Request"));
            }
        }
Ejemplo n.º 3
0
 public void Insert(TopicBindingModel model)
 {
     using (var context = new ForumDatabase())
     {
         context.Topic.Add(CreateModel(model, new Topic()));
         context.SaveChanges();
     }
 }
Ejemplo n.º 4
0
        public ActionResult Create(TopicBindingModel topic)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(topic));
            }

            this.dataService.CreateNewTopic(topic, this.User);
            return(this.RedirectToAction("Index", "Topic"));
        }
Ejemplo n.º 5
0
        public void Delete(TopicBindingModel model)
        {
            var element = topicStorage.GetElement(new TopicBindingModel {
                Name = model.Name
            });

            if (element == null)
            {
                throw new Exception("Элемент не найден");
            }
            topicStorage.Delete(model);
        }
Ejemplo n.º 6
0
 public void Update(TopicBindingModel model)
 {
     using (var context = new ForumDatabase())
     {
         var element = context.Topic.FirstOrDefault(rec => rec.Name == model.Name);
         if (element == null)
         {
             throw new Exception("Элемент не найден");
         }
         CreateModel(model, element);
         context.SaveChanges();
     }
 }
Ejemplo n.º 7
0
 public void CreateOrUpdate(TopicBindingModel model)
 {
     if (topicStorage.GetElement(new TopicBindingModel
     {
         Name = model.Name
     }) != null)
     {
         topicStorage.Update(model);
     }
     else
     {
         topicStorage.Insert(model);
     }
 }
Ejemplo n.º 8
0
 public List <TopicViewModel> Read(TopicBindingModel model)
 {
     if (model == null)
     {
         return(topicStorage.GetFullList());
     }
     if (!model.Name.Equals(null))
     {
         return(new List <TopicViewModel> {
             topicStorage.GetElement(model)
         });
     }
     return(topicStorage.GetFilteredList(model));
 }
Ejemplo n.º 9
0
 public void CreateNewTopic(TopicBindingModel topic, IPrincipal user)
 {
     using (var context = this.GetDbContext)
     {
         context.Topics.Add(new Topic
         {
             Category  = topic.Category,
             UserId    = user.Identity.GetUserId(),
             TopicDate = DateTime.UtcNow,
             Text      = topic.Text,
             Title     = topic.Title
         });
         context.SaveChanges();
     }
 }
Ejemplo n.º 10
0
 public void Delete(TopicBindingModel model)
 {
     using (var context = new ForumDatabase())
     {
         var element = context.Topic.FirstOrDefault(rec => rec.Name == model.Name);
         if (element != null)
         {
             context.Topic.Remove(element);
             context.SaveChanges();
         }
         else
         {
             throw new Exception("Элемент не найден");
         }
     }
 }
Ejemplo n.º 11
0
 public TopicViewModel GetElement(TopicBindingModel model)
 {
     if (model == null)
     {
         return(null);
     }
     using (var context = new ForumDatabase())
     {
         var topic = context.Topic.Include(rec => rec.Message).FirstOrDefault
                         (rec => rec.Name == model.Name);
         return(topic != null ? new TopicViewModel
         {
             Name = topic.Name,
             NumberOfVisitors = topic.Numberofvisitors,
             NumberOfMessages = topic.Numberofmessages
         } :
                null);
     }
 }
Ejemplo n.º 12
0
        public async Task <IHttpActionResult> CreatePost([FromBody] TopicBindingModel model)
        {
            try
            {
                ApplicationUser user = this.userRepository.GetOneById(User.Identity.GetUserId());

                if (user == null)
                {
                    return(BadRequest("Bad Token! No User present!"));
                }

                repository.CreateOne(new Topic {
                    Title = model.Title, CreationDate = model.CreationDate, isPrivate = model.isPrivate, creatorId = user.Id
                });
                return(Ok("Topic Created"));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
Ejemplo n.º 13
0
 public List <TopicViewModel> GetFilteredList(TopicBindingModel model)
 {
     throw new NotImplementedException();
 }