Пример #1
0
        public async Task <IActionResult> Make(string userid)
        {
            User theNewFriend = await userManager.FindByIdAsync(userid);

            User thisUser = await userManager.GetUserAsync(HttpContext.User);

            if (theNewFriend is null || thisUser is null)
            {
                throw new Exception();
            }

            Friend newFriendship = new Friend
            {
                Receiver = theNewFriend,
                Sender   = thisUser,
                Accept   = false
            };

            dataRepository.AddEntity(newFriendship);
            if (dataRepository.SaveAll())
            {
                logger.LogInformation("Ok a new friend request was created");
            }
            ;
            return(RedirectToAction("Personal", "Home", new { username = theNewFriend.UserName }));
        }
Пример #2
0
        public async Task <IActionResult> AddToPost([FromBody] CommentModel CommentData)
        {
            var thisUser = await userManager.GetUserAsync(HttpContext.User);

            if (ModelState.IsValid)
            {
                Post     commentedPost = dataRepository.GetPostById(CommentData.PostID);
                DateTime timeStamp     = DateTime.Now;
                Comment  newComment    = new Comment
                {
                    User        = thisUser,
                    CommentDate = timeStamp,
                    CommentText = CommentData.CommentText,
                    Post        = commentedPost
                };

                dataRepository.AddEntity(newComment);

                if (dataRepository.SaveAll())
                {
                    logger.LogInformation("New comment saved to database");
                }
                ;
                Comment savedComment = await dataRepository.GetCommentByDate(timeStamp);

                return(Json(savedComment.CommentID));
            }
            return(BadRequest("Something bad happened"));
        }
Пример #3
0
        public async Task <ActionResult <Customer> > PostCustomer(Customer customer)
        {
            _repository.AddEntity(customer);
            await _repository.SaveAllAsync();

            return(CreatedAtAction("GetCustomer", new { id = customer.CustomerId }, customer));
        }
Пример #4
0
        public IActionResult Create([FromBody] string newInterest)
        {
            Interest theNewInterest = new Interest
            {
                InterestCategory = newInterest
            };

            dataRepository.AddEntity(theNewInterest);
            if (dataRepository.SaveAll())
            {
                logger.LogInformation("Ok new interest was created and saved to database");
            }
            ;
            return(Ok("Interest Created"));
        }
Пример #5
0
        public IActionResult Post([FromBody] CarViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newCar = _mapper.Map <CarViewModel, Car>(model);

                    Maker manu = _repository.GetMakerById(newCar.Make.MakerId);
                    if (manu != null)
                    {
                        newCar.Make = manu;
                    }
                    else
                    {
                        return(BadRequest($"Make : {newCar.Make.Name} does not exist"));
                    }

                    Model mod = _repository.GetModelById(newCar.Model.ModelId);
                    if (mod != null)
                    {
                        newCar.Model = mod;
                    }
                    else
                    {
                        return(BadRequest($"Model : {newCar.Model.Name} does not exist"));
                    }
                    _repository.AddEntity(newCar);
                    if (_repository.SaveAll())
                    {
                        return(Created($"/api/cars/{newCar.Id}", _mapper.Map <Car, CarViewModel>(newCar)));
                    }
                    else
                    {
                        return(BadRequest("Failed to create a car"));
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to create car : {ex}");
                return(BadRequest("Failed to create a car"));
            }
        }
Пример #6
0
        public async Task <IActionResult> SendMessage([FromBody] SendMessageModel messageModel)
        {
            User receiver = await userManager.FindByIdAsync(messageModel.ReceiverID);

            User thisUser = await userManager.GetUserAsync(HttpContext.User);

            if (receiver is null || thisUser is null)
            {
                throw new Exception("Cannot have null receiver or sender");
            }
            DateTime timestamp  = DateTime.Now;
            Message  newMessage = new Message
            {
                isUnread    = true,
                MessageDate = timestamp,
                MessageText = messageModel.MessageText,
                Receiver    = receiver,
                Sender      = thisUser
            };

            dataRepository.AddEntity(newMessage);

            if (dataRepository.SaveAll())
            {
                logger.LogError("Ok new message was saved");
            }
            ;

            NewMessageModel returnData = new NewMessageModel
            {
                MessageID   = dataRepository.GetMessageIDByTimestampAndUser(timestamp, thisUser),
                MessageDate = timestamp.ToString()
            };

            return(Json(returnData));
        }
Пример #7
0
        public virtual IActionResult AddEntities([FromBody] T[] entities)
        {
            try
            {
                Instance.Context = new zModel();
                Instance.BeginTransaction();
                if (entities != null && entities.Length > 0)
                {
                    foreach (T entry in entities)
                    {
                        Instance.AddEntity(entry);
                    }
                }
                else
                {
                    return(BadRequest());
                }
                Instance.SaveAll();
                if (Instance.CommitTransaction())
                {
                    foreach (var entity in entities)
                    {
                        Instance.Context.Entry <T>(entity).Reload();
                    }
                }

                return(StatusCode(201, entities));
            }
            catch (Exception ex)
            {
                Instance.RollbackTransaction();
                Instance.Logger.LogError(new EventId(), ex, $"Add {typeof(T).Name} Error");
                ModelState.AddModelError("Exception", ex.Message);
                return(BadRequest(ModelState));
            }
        }
Пример #8
0
        public async Task <IActionResult> Create([FromForm] CreatePostViewModel newPostData)
        {
            var thisUser = await userManager.GetUserAsync(HttpContext.User);

            if (ModelState.IsValid)
            {
                List <Interest> addedInterests = new List <Interest>();
                if (!(newPostData.Interests[0] is null))
                {
                    List <Interest> interests = (List <Interest>)dataRepository.GetAllInterests();
                    foreach (Interest interest in interests)
                    {
                        if (newPostData.Interests[0].Contains(interest.InterestCategory))
                        {
                            addedInterests.Add(interest);
                        }
                    }
                }

                DateTime timeStamp  = DateTime.Now;
                Post     theNewPost = new Post
                {
                    User      = thisUser,
                    PostDate  = timeStamp,
                    PostText  = newPostData.PostText,
                    PostScope = (Scope)newPostData.PostScope
                };

                dataRepository.AddEntity(theNewPost);

                if (dataRepository.SaveAll())
                {
                    logger.LogError("Ok new post was saved");
                }
                ;
                Post savedPost = dataRepository.GetPostByTimeStamp(timeStamp);

                var image = newPostData.PostImage;
                if (image != null && image.Length > 0)
                {
                    if (image.ContentType.Contains("png") || image.ContentType.Contains("jpg") || image.ContentType.Contains("jpeg"))
                    {
                        var filePath = env.WebRootPath + "\\postImages\\" + savedPost.PostID + '.' + image.ContentType.Split('/')[1];

                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            await image.CopyToAsync(stream);
                        }
                        savedPost.PostImage = "/postImages/" + savedPost.PostID + '.' + image.ContentType.Split('/')[1];
                    }
                }

                foreach (Interest interest in addedInterests)
                {
                    PostInterest postInterest = new PostInterest
                    {
                        Interest = interest,
                        Post     = savedPost
                    };
                    dataRepository.AddEntity(postInterest);
                }
                if (dataRepository.SaveAll())
                {
                    logger.LogError("Ok relationship of post and interests was saved");
                }
                ;
                return(Ok("Ok new post was saved"));
            }
            return(BadRequest("Something was missing"));
        }
Пример #9
0
 public void AddMenuCategory(MenuCategory menuCategory)
 => _catRepo.AddEntity(menuCategory);
Пример #10
0
 public void AddMenu(Menu menu)
 => _menuRepo.AddEntity(menu);
Пример #11
0
 public void AddMenuItem(MenuItem menuItem)
 => _itemRepo.AddEntity(menuItem);
Пример #12
0
 public void AddRestaurant(Restaurant restaurant)
 => _restRepo.AddEntity(restaurant);
Пример #13
0
 public void CreateUser(OrdUser ordUser)
 => _userRepo.AddEntity(ordUser);
Пример #14
0
 public void AddOrder(Order order)
 => _orderRepo.AddEntity(order);
 public void AddServiceArea(ServiceArea serviceArea)
 => _serviceAreaRepo.AddEntity(serviceArea);
Пример #16
0
 public void AddOrderInfo(OrderInfo orderInfo)
 => _orderInfoRepo.AddEntity(orderInfo);
Пример #17
0
 public void AddOrderItem(OrderItem orderItem)
 => _orderItemRepo.AddEntity(orderItem);