Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.29 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.Http;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.EntityFrameworkCore;
  8. using webapp.Models;
  9.  
  10. namespace webapp.Controllers
  11. {
  12.     [Route("api/[controller]")]
  13.     [ApiController]
  14.     public class FeedbacksController : ControllerBase
  15.     {
  16.         private readonly FeedbackContext _context;
  17.  
  18.         public FeedbacksController(FeedbackContext context)
  19.         {
  20.             _context = context;
  21.         }
  22.  
  23.         //GET: api/Feedbacks
  24.         [HttpGet]
  25.         public async Task<ActionResult<Message>> GetFeedback()
  26.         {
  27.             var feedback = await _context.Messages.FirstOrDefaultAsync(x => x.ID == _context.Messages.Max(p => p.ID));
  28.  
  29.             return feedback;
  30.         }
  31.  
  32.  
  33.         // POST: api/Feedbacks
  34.         // To protect from overposting attacks, please enable the specific properties you want to bind to, for
  35.         // more details see https://aka.ms/RazorPagesCRUD.
  36.         [HttpPost]
  37.         public async Task<ActionResult<Feedback>> PostFeedback(Feedback feedback)
  38.         {
  39.             // _context.Feedback.Add(feedback);
  40.             // Customer ID
  41.             int IdC = 0;
  42.  
  43.             _context.SaveChanges();
  44.             if (null != _context.Customers.FirstOrDefault(x => x.PhoneNumber == feedback.PhoneNumber &&
  45.             x.Mail == feedback.Mail && 0 < x.ID))
  46.             {
  47.                 IdC = _context.Customers.FirstOrDefault(x => x.PhoneNumber == feedback.PhoneNumber &&
  48.             x.Mail == feedback.Mail && 0 < x.ID).ID;
  49.             }
  50.             else
  51.             {
  52.                 _context.Customers.Add(new Customer(feedback.Surname, feedback.Mail, feedback.PhoneNumber));
  53.  
  54.                 _context.SaveChanges();
  55.                 var Cont = _context.Customers.FirstOrDefault(x => x.ID == _context.Customers.Max(p => p.ID));
  56.                 IdC = Cont.ID;
  57.  
  58.             }
  59.             // Theme ID
  60.             int IdT = _context.ThemeList.FirstOrDefault(x => x.ThemeName == feedback.Theme).ID;
  61.             _context.Messages.Add(new Message(feedback.Message, IdC, IdT));
  62.             await _context.SaveChangesAsync();
  63.             return CreatedAtAction("GetFeedback", new { id = feedback.ID }, feedback);
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement