Last change
on this file since c406ae5 was c406ae5, checked in by DimitarSlezenkovski <dslezenkovski@…>, 5 years ago |
Update Models, Repos, Services and Controllers
|
-
Property mode
set to
100644
|
File size:
2.1 KB
|
Rev | Line | |
---|
[de2baac] | 1 | using FarmatikoData.FarmatikoRepoInterfaces;
|
---|
| 2 | using FarmatikoData.Models;
|
---|
[c406ae5] | 3 | using System;
|
---|
[de2baac] | 4 | using System.Collections.Generic;
|
---|
| 5 | using System.Linq;
|
---|
| 6 |
|
---|
| 7 | namespace FarmatikoData.FarmatikoRepo
|
---|
| 8 | {
|
---|
| 9 | public class PharmacyRepository : IPharmacyRepository
|
---|
| 10 | {
|
---|
| 11 | private FarmatikoDataContext _context;
|
---|
| 12 |
|
---|
| 13 | public PharmacyRepository(FarmatikoDataContext context)
|
---|
| 14 | {
|
---|
| 15 | _context = context;
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | public void Add(Pharmacy pharmacy)
|
---|
| 19 | {
|
---|
| 20 | _context.Pharmacies.Add(pharmacy);
|
---|
[c406ae5] | 21 | _context.SaveChanges();
|
---|
[de2baac] | 22 | }
|
---|
[4e72684] | 23 | //Just for users
|
---|
[a55ef91] | 24 | public IQueryable<Pharmacy> GetAll()
|
---|
[4e72684] | 25 | {
|
---|
[c406ae5] | 26 | return _context.Pharmacies.Take(50)
|
---|
| 27 | .Select(x => new Pharmacy
|
---|
| 28 | {
|
---|
| 29 | Name = x.Name,
|
---|
| 30 | Location = x.Location,
|
---|
| 31 | Address = x.Address,
|
---|
| 32 | WorkAllTime = x.WorkAllTime
|
---|
| 33 | }).OrderBy(x => x.Name);
|
---|
[4e72684] | 34 | }
|
---|
[de2baac] | 35 |
|
---|
| 36 | public ICollection<Pharmacy> GetPharmacies()
|
---|
| 37 | {
|
---|
[c406ae5] | 38 | return (ICollection<Pharmacy>)_context.Pharmacies.Take(50)
|
---|
| 39 | .Select(pharmacy => new
|
---|
| 40 | {
|
---|
| 41 | pharmacy.Name,
|
---|
| 42 | pharmacy.Address,
|
---|
| 43 | pharmacy.Location,
|
---|
| 44 | pharmacy.WorkAllTime
|
---|
| 45 | }).OrderBy(x => x.Name);
|
---|
[de2baac] | 46 | }
|
---|
| 47 |
|
---|
| 48 | public void Remove(Pharmacy pharmacy)
|
---|
| 49 | {
|
---|
[c406ae5] | 50 | var pharma = _context.Pharmacies.Where(pharm => pharm.Name.Equals(pharmacy.Name)).FirstOrDefault();
|
---|
| 51 | if (pharma != null)
|
---|
| 52 | {
|
---|
| 53 | _context.Pharmacies.Remove(pharmacy);
|
---|
| 54 | _context.SaveChangesAsync();
|
---|
| 55 | }
|
---|
[de2baac] | 56 | }
|
---|
| 57 |
|
---|
[c406ae5] | 58 | public void UpdatePharmacy(Pharmacy pharmacy)
|
---|
[de2baac] | 59 | {
|
---|
[c406ae5] | 60 | var oldPharmacy = _context.Pharmacies.Where(pharma => pharma.Name.Equals(pharmacy.Name)).FirstOrDefault();
|
---|
| 61 | if (oldPharmacy != null)
|
---|
| 62 | {
|
---|
| 63 | _context.Pharmacies.Remove(oldPharmacy);
|
---|
| 64 | _context.Pharmacies.Add(pharmacy);
|
---|
| 65 | _context.SaveChangesAsync();
|
---|
| 66 | }
|
---|
| 67 | throw new Exception("Pharmacy not found");
|
---|
[de2baac] | 68 | }
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.