Advertisement
Guest User

Untitled

a guest
May 11th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.AspNetCore.Mvc.Rendering;
  7. using Microsoft.EntityFrameworkCore;
  8. using GraniteHouse.Data;
  9. using NotDonkeyApp_UG.Models;
  10. using NotDonkeyApp_UG.Services;
  11. using NotDonkeyApp_UG.Data;
  12.  
  13. namespace NotDonkeyApp_UG.Controllers
  14. {
  15.     public class AnimalNotDonkeysController : Controller
  16.     {
  17.         private readonly ApplicationDbContext _db;
  18.  
  19.         public AnimalNotDonkeysController(ApplicationDbContext db)
  20.         {
  21.             _db = db;
  22.         }
  23.  
  24.         public IActionResult Index()
  25.         {
  26.             var listOfAllUsers = _db.NotDonkeys.ToList();
  27.  
  28.             return View(listOfAllUsers);
  29.         }
  30.  
  31.         public IActionResult AllAnimals()
  32.         {
  33.             if (!StaticDetails.DonkeysAvailableToLike.Any())
  34.             {
  35.                 var listOfAllUsers = _db.NotDonkeys.ToList();
  36.  
  37.                 StaticDetails.DonkeysAvailableToLike = listOfAllUsers;
  38.  
  39.                 return View(listOfAllUsers);
  40.             }
  41.             else
  42.             {
  43.                 return View(StaticDetails.DonkeysAvailableToLike);
  44.             }
  45.         }
  46.  
  47.         public IActionResult AddAnimalToFavourites(int id)
  48.         {
  49.             var user = _db.NotDonkeys.Find(StaticDetails.CurrentUserId);
  50.             if (user != null)
  51.             {
  52.                 user.AnimalsYouLike += $"{id.ToString()}.";
  53.                 StaticDetails.DonkeysAvailableToLike.Remove(StaticDetails.DonkeysAvailableToLike[id]);
  54.                 _db.SaveChanges();
  55.             }
  56.             return RedirectToAction("AllAnimals");
  57.         }
  58.  
  59.         public IActionResult PairedAnimals()
  60.         {
  61.             var user = _db.NotDonkeys.Find(StaticDetails.CurrentUserId);
  62.             if (user != null)
  63.             {
  64.                 var allAnimalLikedids = AnimalService.Instance.ProceedUserIds(user.AnimalsYouLike ?? String.Empty);
  65.                 List<AnimalNotDonkey> yourAnimalsList = new List<AnimalNotDonkey>();
  66.  
  67.                 foreach (var id in allAnimalLikedids)
  68.                 {
  69.                     yourAnimalsList.Add(_db.NotDonkeys.Find(id));
  70.                 }
  71.  
  72.                 return View(yourAnimalsList);
  73.             }
  74.  
  75.             return View(new List<AnimalNotDonkey>());
  76.         }
  77.  
  78.         public IActionResult Login()
  79.         {
  80.             return View();
  81.         }
  82.  
  83.         [HttpPost]
  84.         public IActionResult Login(int ownerPhoneNumber, string password)
  85.         {
  86.             var user = _db.NotDonkeys.FirstOrDefault(x => x.OwnerPhoneNumber == ownerPhoneNumber);
  87.  
  88.             if (user.Password == password)
  89.             {
  90.                 StaticDetails.CurrentUserId = user.Id;
  91.  
  92.                 return RedirectToAction("PairedAnimals");
  93.             }
  94.  
  95.             return View();
  96.         }
  97.  
  98.         public IActionResult Create()
  99.         {
  100.             return View();
  101.         }
  102.  
  103.         [HttpPost]
  104.         [ValidateAntiForgeryToken]
  105.         public async Task<IActionResult> Create([Bind("Id,OwnerPhoneNumber,Name,Password,AnimalType,Sex,Weight,AdditionalDescription,IsDonkey,AnimalsYouLike")] AnimalNotDonkey animalNotDonkey)
  106.         {
  107.             TryCatch(() => {
  108.                 if (ModelState.IsValid && !animalNotDonkey.IsDonkey)
  109.                 {
  110.                     animalNotDonkey.AnimalType = AnimalService.Instance.SetAnimalType(animalNotDonkey.AnimalType);
  111.                     _db.Add(animalNotDonkey);
  112.                     await _db.SaveChangesAsync();
  113.                     return View("Login");
  114.                 }
  115.                 return View(animalNotDonkey);
  116.             }, "wyjebal sie Create");
  117.         }
  118.  
  119.         public IActionResult ThrowErrorMessage()
  120.         {
  121.             return View(StaticDetails.CurrentErrorMessage);
  122.         }
  123.  
  124.         #region Helper Methods
  125.         private void TryCatch(Action metoda, string errorToDisplay)
  126.         {
  127.             try
  128.             {
  129.                 StaticDetails.CurrentErrorMessage = String.Empty;
  130.                 metoda.Invoke();
  131.             }
  132.             catch (Exception e)
  133.             {
  134.                 StaticDetails.CurrentErrorMessage = errorToDisplay + " reason : " + e.Message;
  135.                 return RedirectToAction("ThrowErrorMessage");
  136.             }
  137.         }
  138.         #endregion
  139.     }
  140. }
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157. @model IEnumerable<AnimalNotDonkey>
  158. @{
  159.     ViewData["Title"] = "AllAnimals";
  160. }
  161.  
  162. <h1>List of our current animals : </h1>
  163. <br />
  164.  
  165. <table class="table table-bordered" width="100%">
  166.     <tr>
  167.         <th>Name</th>
  168.         <th>Breed</th>
  169.         <th>avatar</th>
  170.         <th></th>
  171.     </tr>
  172.  
  173.     @foreach (var animal in Model)
  174.     {
  175.         <tr>
  176.             <td>@animal.Name</td>
  177.             <td>@animal.AnimalType</td>
  178.             <td>tu ma byc zdj</td>
  179.             @if (animal.Id != StaticDetails.CurrentUserId)
  180.             {          
  181.               <td><a asp-controller="AnimalNotDonkeys" asp-action="AddAnimalToFavourites" asp-route-Id="@animal.Id">Like It</a></td>
  182.               <td><a asp-controller="AnimalNotDonkeys" asp-action="AddAnimalToFavourites" asp-route-Id="@animal.Id">Like It</a></td>
  183.             }
  184.         </tr>
  185.     }
  186. </table>
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197. using NotDonkeyApp_UG.Models;
  198. using System;
  199. using System.Collections.Generic;
  200. using System.Linq;
  201. using System.Threading.Tasks;
  202.  
  203. namespace NotDonkeyApp_UG.Data
  204. {
  205.     public static class StaticDetails
  206.     {
  207.         /// <summary>
  208.         /// Currently logged user info
  209.         /// </summary>
  210.         public static int CurrentUserId { get; set; }
  211.  
  212.         /// <summary>
  213.         /// AvatarUrl + AnimalType from AnimalNotDonkeyModel == path to img
  214.         /// </summary>
  215.         public static readonly string AvatarUrl = "";
  216.  
  217.         /// <summary>
  218.         /// List will be edited after you like some animal
  219.         /// </summary>
  220.         public static List<AnimalNotDonkey> DonkeysAvailableToLike = new List<AnimalNotDonkey>();
  221.  
  222.         /// <summary>
  223.         /// Will be assigned in catch block and displayed in ThrowErrorMessage View
  224.         /// </summary>
  225.         public static string CurrentErrorMessage { get; set; }
  226.     }
  227. }
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239. TO JEST DO VIEW ThrowErrorMessage
  240. @model string
  241. @{
  242.     ViewData["Title"] = "Login";
  243. }
  244.  
  245. <h1>An Error occured</h1>
  246. <br />
  247. <h2>@model</h2>
  248. <br /><br />
  249. <img/> // tutaj ma byc zdjecie osiolka
  250. <br /><br />
  251. <a asp-controller="AnimalNotDonkeys" asp-action="Index" >Back To Main Page</a>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement